递归深度受限制的旅行目录树 [英] Travel directory tree with limited recursion depth
问题描述
我需要递归处理目录树中的所有文件,但是深度有限。
I need to process all files in a directory tree recursively, but with a limited depth.
例如,这意味着在当前目录和前两个子目录级别中查找文件,但不再查找文件。在这种情况下,我必须处理 ./ subdir1 / subdir2 / file
,但不是 ./ subdir1 / subdir2 / subdir3 / file
。
That means for example to look for files in the current directory and the first two subdirectory levels, but not any further. In that case, I must process e.g. ./subdir1/subdir2/file
, but not ./subdir1/subdir2/subdir3/file
.
如何在Python 3中做到最好?
How would I do this best in Python 3?
当前我使用的是 os.walk
可以在这样的循环中处理所有文件,直至达到无限深度:
Currently I use os.walk
to process all files up to infinite depth in a loop like this:
for root, dirnames, filenames in os.walk(args.directory):
for filename in filenames:
path = os.path.join(root, filename)
# do something with that file...
我可以想到一种计算目录分隔符的方法( /
)在 root
根目录中,以确定当前文件的层次结构级别,如果该级别超过所需的最大值,则在 break
中循环。
I could think of a way counting the directory separators (/
) in root
to determine the current file's hierarchical level and break
the loop if that level exceeds the desired maximum.
当存在大量要忽略的子目录时,我认为这种方法可能不安全并且效率很低。
I consider this approach as maybe insecure and probably pretty inefficient when there's a large number of subdirectories to ignore. What would be the optimal approach here?
推荐答案
我认为最简单,最稳定的方法是复制 os.walk
直接,然后插入自己的深度控制参数。
I think the easiest and most stable approach would be to copy the functionality of os.walk
straight out of the source and insert your own depth-controlling parameter.
import os
import os.path as path
def walk(top, topdown=True, onerror=None, followlinks=False, maxdepth=None):
islink, join, isdir = path.islink, path.join, path.isdir
try:
names = os.listdir(top)
except OSError, err:
if onerror is not None:
onerror(err)
return
dirs, nondirs = [], []
for name in names:
if isdir(join(top, name)):
dirs.append(name)
else:
nondirs.append(name)
if topdown:
yield top, dirs, nondirs
if maxdepth is None or maxdepth > 1:
for name in dirs:
new_path = join(top, name)
if followlinks or not islink(new_path):
for x in walk(new_path, topdown, onerror, followlinks, None if maxdepth is None else maxdepth-1):
yield x
if not topdown:
yield top, dirs, nondirs
for root, dirnames, filenames in walk(args.directory, maxdepth=2):
#...
如果您对所有这些可选参数都不感兴趣,则可以大幅缩减该功能:
If you're not interested in all those optional parameters, you can pare down the function pretty substantially:
import os
def walk(top, maxdepth):
dirs, nondirs = [], []
for name in os.listdir(top):
(dirs if os.path.isdir(os.path.join(top, name)) else nondirs).append(name)
yield top, dirs, nondirs
if maxdepth > 1:
for name in dirs:
for x in walk(os.path.join(top, name), maxdepth-1):
yield x
for x in walk(".", 2):
print(x)
这篇关于递归深度受限制的旅行目录树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!