模式
|
含義
|
*
|
匹配所有
|
?
|
匹配任何單個字符
|
[seq]
|
匹配 seq 中的任何字符
|
[!seq]
|
匹配任何不在 seq 中的字符
|
-
fnmatch.fnmatch(filename, pattern)
import fnmatch
import os
for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print(file)
-
fnmatch.fnmatchcase(filename, pattern)
-
fnmatch.filter(names, pattern)
-
fnmatch.translate(pattern)
示例:
>>> import fnmatch, re
>>>
>>> regex = fnmatch.translate('*.txt')
>>> regex
'(?s:.*\\.txt)\\Z'
>>> reobj = re.compile(regex)
>>> reobj.match('foobar.txt')
<_sre.SRE_Match object; span=(0, 10), match='foobar.txt'>