#需要检查一个“目录”,或者某个包含子目录的目录树,并根据某种模式迭代所有的文件(也可能包含子目录)
使用的是os.walk生成器
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('/home/hzhida','*.py;*html;*pdf'):
print path