使用 fnmatch.filter 按多个可能的文件扩展名过滤文件
给定以下一段 python 代码:
for root, dirs, files in os.walk(directory):
for filename in fnmatch.filter(files, '*.png'):
pass
如何过滤多个扩展?在这种特殊情况下,我想获取所有以 *.png、*.gif、*.jpg 或 *.jpeg 结尾的文件。
现在我想出了
for root, dirs, files in os.walk(directory):
for extension in ['jpg', 'jpeg', 'gif', 'png']:
for filename in fnmatch.filter(files, '*.' + extension):
pass
但我认为它不是很优雅和高性能。
有人有更好的主意吗?
Given the following piece of python code:
for root, dirs, files in os.walk(directory):
for filename in fnmatch.filter(files, '*.png'):
pass
How can I filter for more than one extension? In this special case I want to get all files ending with *.png, *.gif, *.jpg or *.jpeg.
For now I came up with
for root, dirs, files in os.walk(directory):
for extension in ['jpg', 'jpeg', 'gif', 'png']:
for filename in fnmatch.filter(files, '*.' + extension):
pass
But I think it is not very elegant and performant.
Someone has a better idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
如果您只需要检查扩展名(即不需要进一步的通配符),为什么不简单地使用基本的字符串操作呢?
If you only need to check extensions (i.e. no further wildcards), why don't you simply use basic string operations?
我认为你的代码实际上很好。如果您只想对每个文件名进行一次处理,请定义您自己的过滤函数:
I think your code is actually fine. If you want to touch every filename only once, define your own filtering function:
这将是一个更好的方法,也许是因为您没有重复调用
+
并使用tuple
而不是list
。元组更好,因为一旦创建了扩展,您就不会再修改它们。您只是用来迭代它们。
This would be a better way, perhaps because you are not calling
+
repeatedly and using atuple
instead oflist
.A
tuple
is better because you are not going to modify the extension once you have created them. You are just using to iterate over them.我一直在使用这个并取得了很大的成功。
示例:
产量:
使用多种模式进行测试:
产量:
I've been using this with a lot of success.
Examples:
yields:
Testing with multiple patterns:
yields:
这也不是很优雅,但它有效:
This isn't really elegant either, but it works:
在内部,
fnmatch
使用正则表达式。还有一种方法可以根据 fnmatch 模式生成正则表达式 -fnmatch.translate
。这也可能会带来一点加速。Internally,
fnmatch
users regular expressions. And there's a method that makes a regex from an fnmatch pattern —fnmatch.translate
. This may also give a little speed-up.这是我用来过滤 apache 日志目录中的文件的内容。
这里我排除错误文件
Here is what I am using to filter files in apache log directories.
Here I exclude errors flles
请尝试这个:
Please try this:
您可以使用列表理解来检查
my_file
是否与patterns
中定义的任何文件掩码匹配:You can use a list comprehension to check if
my_file
matches any of the file masks defined inpatterns
:最清晰的解决方案是:
或者,使用
pathlib
,The clearest solution is:
or, using
pathlib
,