Python 使用fnmatch.fnmatch(路径“*”)-它匹配所有内容吗?
我继承了一些使用os.walk遍历文件系统各部分的代码Python 使用fnmatch.fnmatch(路径“*”)-它匹配所有内容吗?,python,python-2.7,glob,Python,Python 2.7,Glob,我继承了一些使用os.walk遍历文件系统各部分的代码 for (dirpath, _, filenames) in os.walk(blahblah): reldir = dirpath[len(base)+1:] if fnmatch(reldir, './lost+found'): continue for path in filenames: if fnmatch.fnmatch(path, "*"): .
for (dirpath, _, filenames) in os.walk(blahblah):
reldir = dirpath[len(base)+1:]
if fnmatch(reldir, './lost+found'):
continue
for path in filenames:
if fnmatch.fnmatch(path, "*"):
...
我无法理解使用fnmatch来匹配“*”的意义,是否有一些东西是不匹配的
我使用“、”、“.hidden”、“normal.name”、“normal”
和类似工具运行了一些测试,但似乎没有任何东西被过滤掉
我在文档中看不到任何内容,我猜添加这行是有原因的,有人能告诉我吗?是的,它匹配所有内容。如果您跟踪
fnmatch.fnmatch
的源代码,它可以归结为模式上的正则表达式匹配
In [4]: fnmatch.translate('*')
Out[4]: '.*\\Z(?ms)'
匹配0个或多个字符,后跟字符串结尾(\Z
),并启用多行和点所有标志。这将匹配任何字符串
也许在某个时候,这条线
if fnmatch.fnmatch(path, "*"):
使用了更复杂的模式,但后来改为“*”
,而不是省略检查。但这只是猜测
在任何情况下,都可以删除if条件
,因为它始终为真