列出 python 目录中子文件夹的所有分支

List all branches of subfolders in directory with python

我正在尝试创建目录中所有文件夹和子文件夹以及子子(等)文件夹的列表。到目前为止,我已经想出了这个:

roots = list(set([a[0] for a in tuple(os.walk(r"C:\example"))]))

虽然速度有点慢,主要是os.walk目录中的文件很多。似乎必须有一种更好的方法来确定这一点,即跳过查看所有文件和文件夹。有吗?

看看os.walk的实现:

islink, join, isdir = path.islink, path.join, path.isdir

# We may not have read permission for top, in which case we can't
# get a list of the files the directory contains.  os.path.walk
# always suppressed the exception then, rather than blow up for a
# minor reason when (say) a thousand readable directories are still
# left to visit.  That logic is copied here.
try:
    # Note that listdir and error are globals in this module due
    # to earlier import-*.
    names = listdir(top)
except error, 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)  # not interesting

if topdown: # not inretesting
    yield top, dirs, nondirs
for name in dirs:
    new_path = join(top, name)
    if followlinks or not islink(new_path): # not interesting
        for x in walk(new_path, topdown, onerror, followlinks):
            yield x
if not topdown: # not inretesting
    yield top, dirs, nondirs # not inretesting

我用 "not interesting" 标记了您可以优化的行。我认为如果您使用 followlinks = True 并根据您的需要调整这些行,您可能会得到加速。

你的线路也可以更优化:

roots = list(set([a[0] for a in tuple(os.walk(r"C:\example"))]))
roots = [a[0] for a in os.walk(r"C:\example")]

所以你想要的是这个:

import os
def directory_paths(root):
    isdir = os.path.isdir
    for entry in os.listdir(root):
        new_root = os.path.join(root, entry)
        if isdir(new_root):
            yield new_root
            for path in directory_paths(new_root):
                yield path