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

来源:互联网 发布:投资淘宝众筹怎么赚钱 编辑:程序博客网 时间:2024/05/21 06:36
import os.path as ospEnumTypes=(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

0 0
原创粉丝点击