[英]How can I speed up this directory walk?
I'm trying to write a function that finds a directory by traversing a directory tree with os.walk()
below.我正在尝试编写一个函数,通过下面的os.walk()
遍历目录树来查找目录。 On my machine, this takes 15 seconds.在我的机器上,这需要 15 秒。
for dir_path, dir_names, filenames in os.walk(os.path.expanduser('~')):
for dir_name in dir_names:
if dir_name == 'some_dir':
path = os.path.join(dir_path, dir_name)
print(path)
I read that os.scandir()
is faster so I tried this below, though I think the implementation is wrong.我读到os.scandir()
更快,所以我在下面尝试了这个,尽管我认为实现是错误的。 It works but it's now nearly 30 seconds.它有效,但现在将近 30 秒。
for dir_path, dir_names, filenames in os.walk(os.path.expanduser('~')):
with os.scandir(dir_path) as entries:
for entry in entries:
if entry.name.endswith('some_dir') and entry.is_dir():
print(entry.path)
How can I speed this up?我怎样才能加快速度?
One suggestion would be to replace the for dir_name in dir_names
part:一个建议是替换for dir_name in dir_names
部分中的for dir_name in dir_names
:
for dir_path, dir_names, filenames in os.walk(os.path.expanduser('~'):
if 'some_dir' in dir_names:
path = os.path.join(dir_path, 'some_dir')
print(path)
I do not know if you have lots of subdirectories, but depending on that, this should already make the code faster.我不知道你是否有很多子目录,但取决于它,这应该已经使代码更快了。
Additionally, I would suggest to comment out th另外,我建议注释掉
The os.scandir()
function is preferred over os.listdir
, when you need additional information of the file type, but as dir_names
only contains the directories of the subfolder, this is additional overhead you produce by calling that function, hence it is a lot slower than the original code. os.scandir()
函数优于os.listdir
,当您需要文件类型的附加信息时,但由于dir_names
仅包含子文件夹的目录,这是您通过调用该函数产生的额外开销,因此它是比原始代码慢很多。
If you use a python version >=3.5
, then os.walk()
already calls os.scandir()
under the hood.如果您使用 python 版本>=3.5
,那么os.walk()
已经在os.scandir()
调用os.scandir()
。 As Charles Duffy already mentioned, individually calling os.scandir()
recursively will most likely not be much faster.正如 Charles Duffy 已经提到的,单独调用os.scandir()
很可能不会更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.