您的位置:首页 > 编程语言 > Python开发

Python 查找指定文件夹下的所有路径

2015-03-12 13:57 447 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐