>>> name = ['zhu','jiang','a.txt']
>>> fnmatch.filter(name,'*.txt')
['a.txt']
相当于
>>> [n for n in name if fnmatch.fnmatch(n,'*.txt')]
['a.txt']

fnmatch模块提供了支持linux风格的通配符。

fnmatch.fnmatch(filename, pattern):测试所给的文件filename是否匹配所给的模式pattern

>>> fnmatch.fnmatch('/root/zhu.txt','*.txt')
True
>>> fnmatch.fnmatch('/root/zhu.txt','*.txt1')
False

fnmatch.fnmatchcase(filename, pattern) :测试是否匹配模式时区分大小写,(用在操作系统不区分大小写的情况下)

>>> fnmatch.fnmatch('/root/zhu.txt','*.Txt')
False

fnmatch.filter(names, pattern) :返回一个列表的子集,等价于

[n for n in names if fnmatch(n, pattern)]

>>> name = ['zhu','jiang','a.txt']
>>> fnmatch.filter(name,'*.txt')
['a.txt']
>>> [n for n in name if fnmatch.fnmatch(n,'*.txt')]
['a.txt']