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
相关文章推荐
- python 查找文件夹下的指定部分路径下的所有文件内匹配的字符 包括子文件夹下的文件
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- python查找指定文件夹下所有文件并按修改时间倒序排列
- python 文件和目录 当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- python查找指定文件夹下所有文件并按修改时间倒序排列的方法
- php 遍历指定路径所有目录与文件夹
- java删除指定路径下所有的文件, 及该文件夹里的所有文件夹,不包括该文件夹
- C# 查找指定文件夹下 指定后缀名的所有文件(文件列表)
- JAVA---实现检索指定路径下的所有的下属文件夹和文件
- 删除指定路径根目录所有文件-python
- 用python查找指定格式或名称的文件及修改指定文件夹名称
- 读取指定文件夹下的所有图片之相对路径
- Python_删除/创建指定目录及其下所有子目录的文件,该文件记录当前文件夹的内容
- C#查找指定文件夹下指定后缀名的所有文件
- C# 查找指定文件夹下指定后缀名的所有文件
- 获取指定路径下文件夹所有文件的大小
- C#查找指定文件夹下指定后缀名的所有文件
- 在指定路径下的所有文件中查找给定的字符串
- 用JAVA扫描指定文件夹下面所有以.txt结尾的文件,并将其绝对路径输出
- C#查找指定文件夹下指定后缀名的所有文件