import os.path as osp
EnumTypes=(list,tuple)
def _getsubdirs(prefdirs, others, maxdepth=5):
"""Returns the list of subdirectories of 'prefdirs' and 'others' up to 'maxdepth'.
Note that 'prefdirs' appear at the beginning of the returned list,
followed by their subdirectories, then 'others', and their subdirectories.
"""
new, dnew = [], {} # dnew exists only for performance (order must be kept in new)
for dirs in (prefdirs, others):
if not type(dirs) in EnumTypes:
dirs=[dirs]
dirs=[osp.realpath(i) for i in dirs if i<>'']
for d in dirs:
if dnew.get(d) is None:
new.append(d)
dnew[d] = 1
if maxdepth > 0:
for d in dirs:
level=len(d.split(osp.sep))
for root, l_dirs, l_nondirs in os.walk(d):
lev=len(root.split(osp.sep))
if lev <= (level + maxdepth):
if dnew.get(root) is None:
new.append(root)
dnew[root] = 1
else:
del l_dirs[:] # empty dirs list so we don't walk needlessly
return new
Python 查找指定文件夹下的所有路径
最新推荐文章于 2024-09-08 12:00:00 发布