fnmatch — 类 Unix 的模式匹配
优质
小牛编辑
129浏览
2023-12-01
简单匹配
# fnmatch_fnmatch.py
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)))
# fnmatch_fnmatchcase.py
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.fnmatchcase(name, pattern)))
过滤
# fnmatch_filter.py
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))
翻译模式
# fnmatch_translate.py
import fnmatch
pattern = 'fnmatch_*.py'
print('Pattern :', pattern)
print('Regex :', fnmatch.translate(pattern))