import fnmatch
import os
import re
def fnmatch_filter_demo(path,pattern):
for path,dir,filelist in os.walk(path):
for name in fnmatch.filter(filelist,pattern):
print os.path.join(path,name)
def fnmatch_demo(path,pattern):
for path,dir,filelist in os.walk(path):
for name in filelist:
if fnmatch.fnmatch(name,pattern):
print os.path.join(path,name)
def re_demo(path,pattern):
pattern=fnmatch.translate(pattern)
for path,dir,filelist in os.walk(path):
for name in filelist:
m=re.search(pattern,name)
if m:
print os.path.join(path,name)
re_demo("/var/","*access*")
文件过滤,借助fnmatch或者通过re进行的
fnmatch 实现shell patterns表匹配字符串或文件名
1.fnmatch.fnmatch(name, pattern)
方法:测试name是否匹配pattern,返回true/false
2.fnmatch.filter(names, pat)
实现列表特殊字符的过滤或筛选,返回符合匹配模式的字符列表,当然names表示的是列表;