文件类型操作系统.行走搜索加速代码



我有一个遍历目录树搜索指定文件类型的文件的函数,它工作得很好,唯一的问题是它可能很慢。谁能提供更多的python建议来加快这个过程呢?

def findbyfiletype (filetype, directory):
"""
    findbyfiletype allows the user to search by two parameters, filetype and directory.
    Example:
        If the user wishes to locate all pdf files with a directory including subdirectories
        then the function would be called as follows:
        findbyfiletype(".pdf", "D:\\")
        this will return a dictionary of strings where the filename is the key and the file path is the value
        e.g.
            {'file.pdf':'c:\folder\file.pdf'}

        note that both parameters filetype and directory must be enclosed in string double or single quotes
        and the directory parameter must use the backslash escape \\  as opposed to  as python will throw a string literal error
"""
indexlist =[]                       #holds all files in the given directory including sub folders
FiletypeFilenameList =[]            #holds list of all filenames of defined filetype in indexlist
FiletypePathList = []               #holds path names to indvidual files of defined filetype
for root, dirs, files in os.walk(directory):
    for name in files:
        indexlist.append(os.path.join(root,name))
        if filetype in name[-5:]:
            FiletypeFilenameList.append(name)
for files in indexlist:
    if filetype in files[-5:]:
        FiletypePathList.append(files)
FileDictionary=dict(zip(FiletypeFilenameList, FiletypePathList))
del indexlist, FiletypePathList, FiletypeFilenameList
return FileDictionary

好的,这就是我最后使用的@Ulrich Eckhardt @Anton和@Cox的组合

import os
import scandir
def findbyfiletype (filetype, directory):
    FileDictionary={}
    for root, dirs, files in scandir.walk(directory):
        for name in files:
            if filetype in name and name.endswith(filetype):
                FileDictionary.update({name:os.path.join(root,name)})
return FileDictionary
正如你所看到的,

已经被重构了,去掉了不必要的列表,一步就创建了字典。@Anton你对scandir模块的建议在一个实例中帮助极大地减少了大约97%的时间,这几乎让我大吃一惊。

我把@Anton列为可接受的答案,因为它总结了我在重构中实际取得的所有成果,但@Ulrich Eckhardt和@Cox都获得了投票,因为你们都非常有帮助

代替os.walk(),您可以使用更快的scandir模块(PEP-471)。

还有一些其他的技巧:

  • 不要使用任意的[-5:]
  • 使用ensdswith()字符串方法或os.path.splitext()字符串方法。
  • 不要建立两个很长的清单,然后做一个字典。直接构建字典
  • 如果转义反斜杠打扰你,使用像'c:/folder/file.pdf'这样的正斜杠。

walk()可能很慢,因为试图覆盖很多东西。我使用了一个简单的变体:

def walk(self, path):
    try:
        l = (os.path.join(path, x) for x in os.listdir(path))
        for x in l:
            if os.path.isdir(x):self.walk(x)
            elif x.endswith(("jpg", "png", "jpeg")):
                self.lf.append(x)
    except PermissionError:pass

这很快,python做了文件系统的本地缓存,所以第二次调用甚至更快。

PS:函数walk是类的成员,显然,这就是为什么"self"在那里。

编辑:在NTFS中,不要使用islink。用try/except更新

但是这只是忽略了你没有权限的目录。你必须运行脚本作为管理员,如果你想让他们列出。

最新更新