您正在设置skip = True
但从未重置skip
,因此一旦您跳过了一个文件名,其余的也会被跳过。此外,一个简单的if filename not in mylist
就足够了,不需要做一个显式的循环。
但是,您想在set
此处使用快速成员资格测试,并且在任何情况下都可以简化您的逻辑:
seen = set()
extensions = ['*.txt', '*.foo', '*.bar']
for dirpath, dirnames, filenames in os.walk(directory):
for ext in extensions:
for filename in fnmatch.filter(filenames, ext):
if filename not in seen:
# do your thing
seen.add(filename)
接下来,我们可以去掉fnmatch.filter
这里的选项,使用.endswith()
会更简单更快:
seen = set()
extensions = ('.txt', '.foo', '.bar')
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith(extensions) and filename not in seen:
# do your thing
seen.add(filename)
.endswith()
可以获取一个字符串元组来查找;在这种情况下,您的扩展序列。
如果您只想考虑没有扩展名的文件名,请在测试之前删除扩展名seen
:
extensions = ('.txt', '.foo', '.bar')
for dirpath, dirnames, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith(extensions):
root, ext = os.path.splitext(filename)
if root in seen: # we have seen this filename without extension already
continue
# do your thing
seen.add(root)