我继承了一些使用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, "*"):
...我不明白用fnmatch来对抗"*“的意义,这有什么不匹配的吗?
我用".", "..", ".hidden", "normal.name", "normal"和类似的工具运行了几个测试,但是似乎没有任何东西被过滤掉。
我看不见文档中的任何内容,我猜这行增加是有原因的,有人能告诉我吗?
发布于 2014-01-27 11:45:31
是的,它匹配所有的东西。如果您通过fnmatch.fnmatch的源代码进行跟踪,它可以归结为模式上的正则表达式匹配。
In [4]: fnmatch.translate('*')
Out[4]: '.*\\Z(?ms)'它匹配0或多个字符,后面跟着字符串的结尾(\Z),并带有多行和DOTALL标志。将与任何字符串匹配。
也许在某一时刻
if fnmatch.fnmatch(path, "*"):使用了更复杂的模式,但后来改为"*",而不是省略检查。但那只是猜测。
在任何情况下,if-condition都可以被删除,因为它始终是True。
https://stackoverflow.com/questions/21379660
复制相似问题