本文介绍了python glob中的括号扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有python 2.7,正在尝试发布:
I have python 2.7 and am trying to issue:
glob('{faint,bright*}/{science,calib}/chip?/')
我没有找到任何匹配,但是从外壳echo {faint,bright*}/{science,calib}/chip?
给出了:
I obtain no matches, however from the shell echo {faint,bright*}/{science,calib}/chip?
gives:
faint/science/chip1 faint/science/chip2 faint/calib/chip1 faint/calib/chip2 bright1/science/chip1 bright1/science/chip2 bright1w/science/chip1 bright1w/science/chip2 bright2/science/chip1 bright2/science/chip2 bright2w/science/chip1 bright2w/science/chip2 bright1/calib/chip1 bright1/calib/chip2 bright1w/calib/chip1 bright1w/calib/chip2 bright2/calib/chip1 bright2/calib/chip2 bright2w/calib/chip1 bright2w/calib/chip2
我的表情出了什么问题?
What is wrong with my expression?
推荐答案
由于Python中的glob()
不支持{}
,因此您可能想要的是类似的
Since {}
aren't supported by glob()
in Python, what you probably want is something like
import os
import re
...
match_dir = re.compile('(faint|bright.*)/(science|calib)(/chip)?')
for dirpath, dirnames, filenames in os.walk("/your/top/dir")
if match_dir.search(dirpath):
do_whatever_with_files(dirpath, files)
# OR
do_whatever_with_subdirs(dirpath, dirnames)
这篇关于python glob中的括号扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!