1

我想要做的是匹配一组文件并整理出我想要的文件(匹配扩展名)忽略我已经使用列表处理过的其他文件

到目前为止我想出的是

mylist = []
extensions = ['*.txt', '*.foo', '*.bar']
for dirpath, dirnames, filenames in os.walk(directory):
    skip = None
    for ext in extensions:
        for filename in fnmatch.filter(filenames, ext):
            for test in mylist:
                if test == filename:
                    skip = True
            if not skip:
                ## do my thing
                mylist.append(filename)

但它忽略了我的 if 测试语句。我要瞎了吗?

4

1 回答 1

2

您正在设置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)
于 2013-05-10T09:03:43.740 回答