安迪_963

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

通常情况下如果要进行比较复杂的匹配,可以考虑用正则表达式模块:re模块,这里介绍fnmatch模块:

#!/usr/bin/env python
#coding:utf-8
#@Author:Andy

from fnmatch import fnmatch ,fnmatchcase
res1= fnmatch("foot.txt", '*.txt')
print(res1) # True,
res2 = fnmatch("foo.txt", "?oo.txt")
print(res2) # True

# sometimes you need to filter some kind of str
file_name = ['Dat1.csv', 'Dat2.csv', 'config.ini', 'foo.py']
res3 = [name for name in file_name if fnmatch(name, "*.csv")]
print(res3) # ["dat1.csv", "Dat2.csv"]
# The fnmatch use the same case sensitivity
# on linux  case sensitive , but not on windows
# if you want to set the sensitivity you'd better use fnmatchcase

res4 = fnmatchcase('test.txt', '*.TXT')
print(res4) # False

 

posted on 2017-09-20 19:12  Andy_963  阅读(276)  评论(0编辑  收藏  举报