您的位置:首页 > 编程语言 > Python开发

Python 之模块fnmatch

2014-02-28 17:46 429 查看
>>> 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']
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息