一个很好用的,用python实现递归遍历的方法

 

 

 

__auth__='lai zi wang luo'
#coding=utf-8

#所传参数为目录和所查的文件
import os, fnmatch
 
def all_files(root, patterns = '*', single_level = False, yield_folders=False):
    patterns = patterns.split(';')
    for path, subdirs, files in os.walk(root):
        if yield_folders:
            files.extend(subdirs)
        files.sort()
        for name in files:
            for pattern in patterns:
                if fnmatch.fnmatch(name, pattern):
                    yield os.path.join(path,name)
                    break
        if single_level:
            break
 
if __name__ == '__main__':
    for path in all_files('/mnt','*.json;*py'):
        print path

 

 

你可能感兴趣的:(python)