遍历以各种扩展名结尾的文件



我正在使用这行代码迭代以.tar扩展名结尾的文件,使用我认为是regex字符'*'。

for f in glob.glob('{}/{}/Compressed_Files/*.tar'.format(path, site_id)): 

如何既能做同样的事情,又能包含以csv.gz扩展名结尾的文件?可能使用正则表达式或运算符?

glob不支持可以像那样匹配多个字符串的模式。把两个地球仪合在一起。

g1 = glob.glob('{}/{}/Compressed_Files/*.tar'.format(path, site_id))
g2 = glob.glob('{}/{}/Compressed_Files/*.tar.gz'.format(path, site_id))
for f in g1 + g2:
# code

如果有很多匹配,最好使用glob.iglob(),它是一个迭代器。然后使用itertools.chain()将它们组合。

g1 = glob.iglob('{}/{}/Compressed_Files/*.tar'.format(path, site_id))
g2 = glob.iglob('{}/{}/Compressed_Files/*.tar.gz'.format(path, site_id))
for f in itertools.chain(g1, g2):
# code

使用生成器,不会缓存所有结果,可能会有很多文件

def glob_patterns(patterns: list[str]):
for pattern in patterns:
for path in glob.iglob(pattern):
yield path
for path in glob_patterns((
'{}/{}/Compressed_Files/*.{}'.format(path, site_id, extension)
for extension in ('tar', 'tar.gz')
)):
print(path)

最新更新