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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值