Python遍历目录中的所有文件的方法
os.walk生成器 os.path.join(d[0],d[1]); 获得所有子文件,就是: os.path.join(d[0],d[2]); 以下例子使用了两套循环,遍历后得到所有文件名的list后再循环所有文件: result = [os.path.join(dp,f) for dp,dn,fs in os.walk("_pages") for f in fs if os.path.splitext(f)[1] == '.html'] for fname in result: #do something 实际等于 result=[] for dp,fs in os.walk("_pages"): for f in fs: if (os.path.splitext(f)[1] == '.html'): result.append(os.path.join(dp,f)) for fname in result: #do something 最后判断是否html后缀获得文件名,还可以使用glob: result = [y for x in os.walk(PATH) for y in glob.glob(os.path.join(x[0],'*.txt'))] 还可以使用迭代器方法: from itertools import chain import glob result = (chain.from_iterable(glob.iglob(os.path.join(x[0],'*.txt')) for x in os.walk('.'))) 进阶 import os,fnmatch def filter_files(dirname,patterns='*',single_level=False,yield_folders=False): patterns = patterns.split(';') allfiles = [] for rootdir,subdirname,files in os.walk(dirname): print subdirname allfiles.extend(files) if yield_folders: allfiles.extend(dubdirname) if single_level: break allfiles.sort() for eachpattern in patterns: for eachfile in fnmatch.filter(allfiles,eachpattern): print os.path.normpath(eachfile) 说明: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |