gpt4 book ai didi

python - 加快Python中搜索文件夹路径的速度?

转载 作者:行者123 更新时间:2023-12-01 03:26:19 25 4
gpt4 key购买 nike

我正在使用 python 2.7 并尝试搜索文件夹而不是文件

我们为每个项目都有特定的项目编号,并且每个文档都位于任何项目下的项目文件夹中。因此,我们必须搜索项目文件夹是否存在或有效。

我的脚本可以找到特定文件夹,但需要很长时间(大约 11 到 13 分钟才能完成该过程)

我想分享我的脚本工作以获得一些更好的想法。

import os                            

path=r"F:\Projekte"

def find_directory():
for root, dirs, files in os.walk(path):
for i in dirs:
if i=="A9901001":
print root

find_directory()

最佳答案

为什么不只是这个?

def find_directory():
for root, dirs, files in os.walk(path):
if "A9901001" in dirs:
print(root, "A9901001")

如果你有机会使用 python3:有 os.scandir它的功能与 os.walk 类似,但在 NTFS 上速度更快。

关于python - 加快Python中搜索文件夹路径的速度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41376266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com