| 函数名 | 功能 |
|---|---|
| fnmatch.filter(names, pattern) | 对 names 列表进行过滤,返回 names 列表中匹配 pattern 的文件名组成的子集合。 |
| fnmatch.fnmatch(filename, pattern) | 判断 filename 文件名,是否和指定 pattern 字符串匹配 |
| fnmatch.fnmatchcase(filename, pattern) | 和 fnmatch() 函数功能大致相同,只是该函数区分大小写。 |
| fnmatch.translate(pattern) | 将一个 UNIX shell 风格的 pattern 字符串,转换为正则表达式 |
import fnmatch
#filter()
print(fnmatch.filter(['dlsf', 'ewro.txt', 'te.py', 'youe.py'], '*.txt'))
#fnmatch()
for file in ['word.doc','index.py','my_file.txt']:
if fnmatch.fnmatch(file,'*.txt'):
print(file)
#fnmatchcase()
print([addr for addr in ['word.doc','index.py','my_file.txt','a.TXT'] if fnmatch.fnmatchcase(addr, '*.txt')])
#translate()
print(fnmatch.translate('a*b.txt'))
程序执行结果为:
['ewro.txt']
my_file.txt
['my_file.txt']
(?s:a.*b\.txt)\Z
版权说明:Copyright © 广州松河信息科技有限公司 2005-2025 版权所有 粤ICP备16019765号
广州松河信息科技有限公司 版权所有