如何使用os.walk和fnmatch改进搜索 [英] How to improve searching with os.walk and fnmatch

查看:169
本文介绍了如何使用os.walk和fnmatch改进搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 os.walk fnmatch 使用过滤器在PC的硬盘中搜索所有图像文件。这非常好,但速度极慢,因为搜索+ -70000图像需要大约9分钟。

I'm using os.walk and fnmatch with filters to search a pc's hdd for all image files. This works perfectly fine but is extremely slow since it takes about 9 minutes to search +-70000 images.

有关优化此代码以更快运行的任何想法吗?
还有其他任何建议吗?

Any ideas on optimizing this code to run faster? Any other suggestions?

我顺便使用python 2.7.2。

I'm using python 2.7.2 by the way.

import fnmatch
import os

images = ['*.jpg', '*.jpeg', '*.png', '*.tif', '*.tiff']
matches = []

for root, dirnames, filenames in os.walk("C:\\"):
    for extension in images:
        for filename in fnmatch.filter(filenames, extension):
            matches.append(os.path.join(root, filename))


推荐答案

我不是那些总是使用重新锤子解决所有问题,但实际上我的测试速度比你的fnmatch版本高出两倍:

I'm not one of those regex maniacs who always resorts to the re hammer to solve all problems, but this actually ran a wee bit over twice as fast in my tests as your fnmatch version:

import os
import re

matches = []

img_re = re.compile(r'.+\.(jpg|png|jpeg|tif|tiff)$', re.IGNORECASE)

for root, dirnames, filenames in os.walk(r"C:\windows"):
    matches.extend(os.path.join(root, name) for name in filenames if img_re.match(name))

这篇关于如何使用os.walk和fnmatch改进搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆