简体   繁体   English

Python文件搜索程序不够快

[英]Python file search program not fast enough

I wrote the following code for searching a file on your computer: 我编写了以下代码来搜索您计算机上的文件:

import os, sys
import win32api

x=raw_input("Enter file name: ")

drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
for drive in drives:
    for folderName, subfolders, filenames in os.walk(drive):
        for filename in filenames:
            if x.upper() in filename:
                print"FILE FOUND!"
                print('FILE INSIDE ' + folderName + ': '+ filename)
            elif x.lower() in filename:
                print"FILE FOUND!"
                print('FILE INSIDE ' + folderName + ': '+ filename)
            elif x.capitalize() in filename:
                print"FILE FOUND!"
                print('FILE INSIDE ' + folderName +': '+ filename)
a=raw_input("Press any key to exit.")
sys.exit()

As you may have noticed this program is not fast enough. 您可能已经注意到,该程序不够快。

So could anyone help me make a faster and more efficient version of this program? 那么,有谁能帮助我制作一个更快,更高效的程序?

Thanks! 谢谢!

You can't enhance this program much - any static file search will have to do that sort of thing. 您不能对此程序进行太多改进-任何静态文件搜索都必须执行此类操作。 By adding a lot of complexity and making it parallel, you could get it to work faster by walking different portions of the file system at a time, and possibly enabling a "lucky hit" earlier. 通过增加很多复杂性并使其并行,您可以一次遍历文件系统的不同部分,并可能更早地启用“幸运命中”,从而使其运行更快。

Applications and utilities that are designed for fast search of the file system usually resort to indexing all file system contents in a database - and they keep doing that either continuously on the background, or at a fixed time of the day. 专为快速搜索文件系统而设计的应用程序和实用程序通常采用索引数据库中所有文件系统内容的方式,并且它们会在后台连续或在一天的固定时间进行索引。 So, when there is aa search,they just perform a query on that database. 因此,当进行搜索时,他们只需对该数据库执行查询。

This solution however would increase the complexity of your small program several times - and it would be too complex to write here as an answer; 但是,该解决方案将使您的小程序的复杂性提高数倍-太复杂而无法在此处编写答案。

Reducing the number of print statements should increase speed significantly. 减少打印语句的数量应显着提高速度。 If you need to trace the files, you could write your results to a log file instead. 如果需要跟踪文件,则可以将结果写入日志文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM