Skip to content

Latest commit

 

History

History
49 lines (42 loc) · 1.32 KB

day13.md

File metadata and controls

49 lines (42 loc) · 1.32 KB

第十三天

索引

python标准库 fnmatch

  1. 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)))
  1. 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))
  1. translate (转换为标准的正则)
import fnmatch

pattern = 'fnmatch_*.py'
print('Pattern :', pattern)
print('Regex   :', fnmatch.translate(pattern))