Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串中间的Python通配符_Python - Fatal编程技术网

字符串中间的Python通配符

字符串中间的Python通配符,python,Python,我有一个目录,其中包含许多文件,这些文件都具有相同的基本文件名格式(下面是一个示例) 我正在尝试使用以下选项选择文件: filename = 'roll_'+str(roll)'_'*ID 其中roll是一个int,我将其转换为字符串,ID是字符串。我使用通配符*作为_oe_date元素,因为我不关心这部分。但是,我得到以下错误: TypeError: can't multiply sequence by non-int of type 'str' 我如何在中间使用通配符构造文件名? 我想你

我有一个目录,其中包含许多文件,这些文件都具有相同的基本文件名格式(下面是一个示例)

我正在尝试使用以下选项选择文件:

filename = 'roll_'+str(roll)'_'*ID
其中roll是一个int,我将其转换为字符串,ID是字符串。我使用通配符*作为_oe_date元素,因为我不关心这部分。但是,我得到以下错误:

TypeError: can't multiply sequence by non-int of type 'str'

我如何在中间使用通配符构造文件名?

我想你想要这个模块,而<代码> STR.Frad(…)<代码>方法:

# Don't know where this comes from.
roll = 2
id = 'AAC'

# Format: roll_2_oe_2008-04-07_AAJ.XNGS
Fn_format= "roll_{}_oe_*_{}.XNGS"

# Make a file-glob by putting the roll into format.
rolled_files = Fn_format.format(roll, id)


import glob

for fname in glob.iglob(rolled_files):
    print("Rolled file:", fname)
您可以使用
format()


您必须将通配符作为字符嵌入,并确保您的文件搜索界面接受通配符

filename = 'roll_'+str(roll)'_*'+ID

但是,请注意,这并没有标识特定的文件名:为此,您需要一个系统命令(有几个命令用于搜索和获取文件名)。Pythonopen函数不接受通配符

你的问题很难理解。我想你想要
文件名='roll_u'+str(roll)+'+
。你不是说
+
而不是
*
*
需要在一个字符串中,在你使用它作为乘数的那一刻。还要感谢Austin,我如何将ID元素添加到Fn\u格式中(我需要传递ID(AAA或AAG等)在文件名中?谢谢在格式字符串的正确位置添加另一个
{}
,并调用
.format(roll,id)
。有关格式字符串的详细信息,请参阅。如果我理解正确,可能是“roll{}oe{u*.{ucode}.XNGS”。编辑为添加
id
谢谢Jan,我如何将日期元素包括在文件名中?
filename = 'roll_{}_{}'.format(roll, ID)
filename = 'roll_'+str(roll)'_*'+ID