在python上获取大目录文件列表的最佳方法?

fun*_*vit 12 python memory directory iterator list

我有疯狂的大目录.我需要通过python获取文件列表.

在代码中我需要获取迭代器,而不是列表.所以这不起作用:

os.listdir
glob.glob  (uses listdir!)
os.walk
Run Code Online (Sandbox Code Playgroud)

我找不到任何好的lib.救命!也许c ++ lib?

小智 8

如果你的目录太大而不能让libc readdir()快速读取它,你可能想看一下内核调用getdents()(http://www.kernel.org/doc/man-pages/online/pages/man2/getdents.2.html).我遇到了类似的问题,写了一篇关于它的长篇博文.

http://www.olark.com/spw/2011/08/you-can-list-a-directory-with-8-million-files-but-not-with-ls/

基本上,readdir()一次只读取32K的目录条目,因此如果目录中有很多文件,readdir()将需要很长时间才能完成.