我正在尝试创建目录中所有文件夹和子文件夹以及子子(等)文件夹的列表。到目前为止,我已经想出了这个:
roots = list(set([a[0] for a in tuple(os.walk(r"C:\example"))]))
虽然它有点慢,主要是由于目录中有很多文件时os.walk
。似乎必须有一个更好的方法来确定跳过查看所有文件和文件夹的方法。有吗?
答案 0 :(得分:1)
查看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
我标记了可以用“不感兴趣”优化的行。如果您使用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