更快的替代/使用 os.walk 只查找 Python 中的所有子目录
根据我在互联网和此处阅读的内容,os.walk 是查找目录中所有子目录和文件的最佳选择之一,但我的问题是,如果我仅想要递归地查找所有子目录,os.walk是最快的解决方案吗?
我想另外我的问题是,由于对 os.walk 的调用涉及 os.walk(path) 中的 root、dirs、文件,如果您不这样做,os.walk 是否实际上会查看所有文件?不一定需要它吗?
目前,我的代码是:
for root, dirs, files in os.walk(path):
for x in dirs:
DoStuffHere
但在包含许多子目录和文件的文件夹上,它确实看起来很慢。
谢谢。
From what I've read around the interwebs and here on SO, os.walk is one of the best bets for finding all subdirs and files in a directory, but the question I have is, if I only want to recursively find all the subdirectories, is os.walk the fastest solution?
I suppose in addition my question is since the call to os.walk involves for root, dirs, files in os.walk(path)
, does os.walk actually look at all the files if you don't necessarily call for it?
Presently, my code is:
for root, dirs, files in os.walk(path):
for x in dirs:
DoStuffHere
But it sure seems slow on a folder with many many subdirs and files.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对这方面的所有方面都不是 100% 确定,但根据我的理解:
一般来说,文件列表已经包含在目录元数据中,因此当您查找目录时,数据已经在那里(Linux,不确定 Windows) 。所以这意味着 os.walk 可能是最快/最简单的方法。
同样,在没有分析的情况下,您是否知道 os.walk 是否真的是速度变慢的地方?请记住,一般建议是对您的应用程序/项目进行编码,然后如果它太慢,请开始分析以找到缓慢的部分并重新考虑它们,等等......
在 os.walk 上,我能够大致结束 os.walk几秒钟之内就有几千个目录+ 7万个文件,所以它的速度可能足以满足您的需要。
I'm not 100% for certain on all aspects of this but from my understanding:
In general the file listing is already included in the directory metadata so when you look up a directory the data is already there (Linux, not sure bout windows). So this means that os.walk is probably the fastest/simplest way of doing this.
Also again without profiling do you know if os.walk is really where the slow down is? Remember the general advice is to code up your application/project and then if its too slow, start profiling to find the slow part and re-factor them, etc...
On os.walk I'm able to os.walk roughly over a few thousand directory + 70 thousand files within a few second so its going to probably be plenty fast for your need.