比调用“ls”更快地获取目录列表的方法在子流程中
经过搜索,一些测试运行 os.popen()+read() 和 subprocess.check_output() 似乎几乎等同于读出文件夹的内容。有没有办法改进 os.popen()+read() 或 subprocess.check_output() 的组合?我必须ls
多个文件夹并读取输出,使用上述任一方法都是类似的,但根据分析结果代表了主要瓶颈。
After a search, and some test runs both os.popen()+read() and subprocess.check_output() seem to be almost equivalent for reading out the contents of a folder. Is there a way to improve either a combination of os.popen()+read() or subprocess.check_output()? I have to ls
a number of folders and read the outputs, and using either of the above is similar, but represents the major bottleneck according to profiling results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找 os.listdir 和/或 os.walk,也许还有 os.stat 系列函数。这些(Python 绑定)与
ls
使用的原语相同,因此您可以通过解析ls
的输出来执行任何操作。我建议您仔细阅读 os、os.path 和 stat 模块报价;可能还有其他事情您不需要外部程序来做。您可能还应该阅读底层系统调用
stat
的文档 ——它是面向 C 的,但它将帮助您理解 os.stat 的作用。You are looking for os.listdir and/or os.walk, and perhaps also the os.stat family of functions. These are (Python bindings to) the same primitives that
ls
uses itself, so anything you can do by parsing the output ofls
you can do with these. I recommend you read carefully through everything the os, os.path, and stat modules offer; there may be other things you don't need an external program to do.You should probably also read the documentation for
stat
, the underlying system call -- it's C-oriented, but it'll help you understand whatos.stat
does.为什么不直接使用
os.listdir< 直接读取目录内容/代码>
?为什么需要 shell 到
。自己执行系统调用比创建子流程来为您执行要高效得多。ls
?如果您除了文件名之外还需要有关文件的更多信息,还可以使用 <代码>os.stat要进行完全目录遍历,可以使用
os.walk
。shutil
模块也有一些有用的功能。Why don't you just read the directory contents directly with
os.listdir
? Why do you need to shell out tols
? If you need more information about files in addition to just the filenames, you can also useos.stat
. It's much more efficient to do the system calls yourself than to create subprocesses to do it for you.For doing entirely directory traversals, there's
os.walk
. Theshutil
module also has some useful functions.使用 glob:
语法相同。
例如
Use glob:
The syntax is the same.
e.g.