Python 如何使用 os.scandir() 在目录树上递归返回 DirEntry 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33135038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use os.scandir() to return DirEntry objects recursively on a directory tree?
提问by Mark
Python 3.5's os.scandir(path)
function returns lightweight DirEntry objects that are very helpful with information about files. However, it only works for the immediate path handed to it. Is there a way to wrap it in a recursive function so that it visits all subdirectories beneath the given path?
Python 3.5 的os.scandir(path)
函数返回轻量级 DirEntry 对象,这些对象对于文件信息非常有用。但是,它仅适用于传递给它的直接路径。有没有办法将它包装在递归函数中,以便它访问给定路径下的所有子目录?
采纳答案by Ben Hoyt
You can scan recursively using os.walk()
, or if you need DirEntry
objects or more control, write a recursive function like scantree()
below:
您可以使用 递归扫描os.walk()
,或者如果您需要DirEntry
对象或更多控制,请编写scantree()
如下递归函数:
try:
from os import scandir
except ImportError:
from scandir import scandir # use scandir PyPI module on Python < 3.5
def scantree(path):
"""Recursively yield DirEntry objects for given directory."""
for entry in scandir(path):
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path) # see below for Python 2.x
else:
yield entry
if __name__ == '__main__':
import sys
for entry in scantree(sys.argv[1] if len(sys.argv) > 1 else '.'):
print(entry.path)
Notes:
笔记:
- There are a few more examples in PEP 471and in the os.scandir() docs.
- You can also add various logic in the for loop to skip directories or files starting with
'.'
and that kind of thing. - You typically want
follow_symlinks=false
on theis_dir()
calls in recursive functions like this, to avoid symlink loops. On Python 2.x, replace the
yield from
line with:for entry in scantree(entry.path): yield entry
- PEP 471和os.scandir() 文档中还有更多示例。
- 您还可以在 for 循环中添加各种逻辑以跳过以之
'.'
类的内容开头的目录或文件。 - 您通常希望
follow_symlinks=false
在这样的is_dir()
递归函数中进行调用,以避免符号链接循环。 在 Python 2.x 上,将该
yield from
行替换为:for entry in scantree(entry.path): yield entry