002_016 Python 遍历目录树

代码如下:

#encoding=utf-8

print '中国'

#遍历目录树

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

for path in all_files(r'H:\football\GetFootballMatch\Release'):
    print path.decode('gbk')

  
打印结果如下:

中国
H:\football\GetFootballMatch\Release\ft.ini
H:\football\GetFootballMatch\Release\足彩辅助.exe
H:\football\GetFootballMatch\Release\足彩辅助.pdb
H:\football\GetFootballMatch\Release\足彩辅助.zip
H:\football\GetFootballMatch\Release\足彩辅助\足彩辅助.exe



你可能感兴趣的:(002_016 Python 遍历目录树)