- simple match (大小写是否敏感根据系统,如果强制敏感使用 fnmatchcase())
import fnmatch
import os
pattern = 'fnmatch_*.py'
print('Pattern :', pattern)
print()
files = os.listdir('.')
for name in sorted(files):
print('Filename: {:<25} {}'.format(
name, fnmatch.fnmatch(name, pattern)))
- filter 返回列表
import fnmatch
import os
import pprint
pattern = 'fnmatch_*.py'
print('Pattern :', pattern)
files = list(sorted(os.listdir('.')))
print('\nFiles :')
pprint.pprint(files)
print('\nMatches :')
pprint.pprint(fnmatch.filter(files, pattern))
- translate (转换为标准的正则)
import fnmatch
pattern = 'fnmatch_*.py'
print('Pattern :', pattern)
print('Regex :', fnmatch.translate(pattern))