文件类型手术室步行搜索加速鳕鱼

2024-04-24 11:35:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数,它遍历目录树,搜索指定文件类型的文件,工作得很好,唯一的问题是它可能很慢。有人能提供更多的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

好吧,这就是我最后用了,乌尔里希·埃克哈特,安东和考克斯的组合

^{pr2}$

如您所见,它已经被重新考虑,去掉了不必要的列表,并一步创建了字典。@安东,你对scandir模块的建议大大缩短了97%的时间,这几乎让我大吃一惊。在

我将@Anton列为可接受的答案,因为它总结了我在重构方面所取得的实际成果,但是@Ulrich Eckhardt和@Cox都获得了支持,因为你们都很有帮助

问候


Tags: andofthetonameinpdffiles
2条回答

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对文件系统进行本地缓存,所以第二次调用甚至更快。在

注:功能行走是一个类的成员,显然,这就是为什么“自我”存在。在

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

但这只是忽略你没有权限的dir。如果要列出这些脚本,则必须以管理员身份运行脚本。在

您可以使用更快的scandir模块(PEP-471),而不是^{cd1>}。在

另外,还有一些其他提示:

  • 不要使用任意的[-5:]。使用ensdswith()字符串方法或使用os.path.splitext()。在
  • 不要直接列两个单子。在
  • 如果转义反斜杠困扰您,请使用正斜杠,如“c:/folder”/文件.pdf'. 他们只是工作。在

相关问题 更多 >