每周一个 Python 标准库 | fnmatch

技术博客:https://github.com/yongxinz/tech-blog

同时,也欢迎关注我的微信公众号 AlwaysBeta,更多精彩内容等你来。

fnmatch 模块主要用于文件名的比较,使用 Unix shell 使用的 glob 样式模式。

简单匹配

fnmatch() 将单个文件名与模式进行比较并返回布尔值,来看它们是否匹配。当操作系统使用区分大小写的文件系统时,比较区分大小写。

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)))
    
# output
# Pattern : fnmatch_*.py
# 
# Filename: fnmatch_filter.py         True
# Filename: fnmatch_fnmatch.py        True
# Filename: fnmatch_fnmatchcase.py    True
# Filename: fnmatch_translate.py      True
# Filename: index.rst                 False

在此示例中,模式匹配所有以 'fnmatch_' 开头和以 '.py' 结尾的文件。

要强制进行区分大小写的比较,无论文件系统和操作系统设置如何,请使用 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.fnmatchcase(name, pattern)))
    
# output
# Pattern : FNMATCH_*.PY
# 
# Filename: fnmatch_filter.py         False
# Filename: fnmatch_fnmatch.py        False
# Filename: fnmatch_fnmatchcase.py    False
# Filename: fnmatch_translate.py      False
# Filename: index.rst                 False

由于用于测试此程序的 OS X 系统使用区分大小写的文件系统,因此没有文件与修改后的模式匹配。

过滤

要测试文件名序列,使用 filter(),它返回与 pattern 参数匹配的名称列表。

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))

# output
# Pattern : fnmatch_*.py
# 
# Files   :
# ['fnmatch_filter.py',
#  'fnmatch_fnmatch.py',
#  'fnmatch_fnmatchcase.py',
#  'fnmatch_translate.py',
#  'index.rst']
# 
# Matches :
# ['fnmatch_filter.py',
#  'fnmatch_fnmatch.py',
#  'fnmatch_fnmatchcase.py',
#  'fnmatch_translate.py']

在此示例中,filter() 返回与此部分关联的示例源文件的名称列表。

翻译模式

在内部,fnmatchglob 模式转换为正则表达式,并使用 re 模块比较名称和模式。translate() 函数是将 glob 模式转换为正则表达式的公共 API。

import fnmatch

pattern = 'fnmatch_*.py'
print('Pattern :', pattern) # Pattern : fnmatch_*.py
print('Regex   :', fnmatch.translate(pattern))  # Regex   : (?s:fnmatch_.*\.py)\Z

原文链接:

https://pymotw.com/3/fnmatch/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 核心模块 o 1.1. 介绍 o 1.2. _ _builtin_ _ 模块 o 1.3. exceptions 模块 o 1.4. os 模块 o 1.5. os.path 模块 o 1.6. stat 模块 o 1.7. string 模块 o 1.8. re 模块 o 1.9. math 模块 o 1.10. cmath 模块 o 1.11. operator 模块 o 1.12. copy 模块 o 1.13. sys 模块 o 1.14. atexit 模块 o 1.15. time 模块 o 1.16. types 模块 o 1.17. gc 模块 2. 更多标准模块 o 2.1. 概览 o 2.2. fileinput 模块 o 2.3. shutil 模块 o 2.4. tempfile 模块 o 2.5. StringIO 模块 o 2.6. cStringIO 模块 o 2.7. mmap 模块 o 2.8. UserDict 模块 o 2.9. UserList 模块 o 2.10. UserString 模块 www.aibbt.com 让未来触手可及o 2.11. traceback 模块 o 2.12. errno 模块 o 2.13. getopt 模块 o 2.14. getpass 模块 o 2.15. glob 模块 o 2.16. fnmatch 模块 o 2.17. random 模块 o 2.18. whrandom 模块 o 2.19. md5 模块 o 2.20. sha 模块 o 2.21. crypt 模块 o 2.22. rotor 模块 o 2.23. zlib 模块 o 2.24. code 模块 3. 线程和进程 o 3.1. 概览 o 3.2. threading 模块 o 3.3. Queue 模块 o 3.4. thread 模块 o 3.5. commands 模块 o 3.6. pipes 模块 o 3.7. popen2 模块 o 3.8. signal 模块 4. 数据表示 o 4.1. 概览 o 4.2. array 模块 o 4.3. struct 模块 o 4.4. xdrlib 模块 o 4.5. marshal 模块 o 4.6. pickle 模块 o 4.7. cPickle 模块 o 4.8. copy_reg 模块 o 4.9. pprint 模块 o 4.10. repr 模块 o 4.11. base64 模块 o 4.12. binhex 模块 o 4.13. quopri 模块 o 4.14. uu 模块 o 4.15. binascii 模块 5. 文件格式 o 5.1. 概览 o 5.2. xmllib 模块 o 5.3. xml.parsers.expat 模块 o 5.4. sgmllib 模块 www.aibbt.com 让未来触手可及o 5.5. htmllib 模块 o 5.6. htmlentitydefs 模块 o 5.7. formatter 模块 o 5.8. ConfigParser 模块 o 5.9. netrc 模块 o 5.10. shlex 模块 o 5.11. zipfile 模块 o 5.12. gzip 模块 6. 邮件和新闻消息处理 o 6.1. 概览 o 6.2. rfc822 模块 o 6.3. mimetools 模块 o 6.4. MimeWriter 模块 o 6.5. mailbox 模块 o 6.6. mailcap 模块 o 6.7. mimetypes 模块 o 6.8. packmail 模块 o 6.9. mimify 模块 o 6.10. multifile 模块 7. 网络协议 o 7.1. 概览 o 7.2. socket 模块 o 7.3. select 模块 o 7.4. asyncore 模块 o 7.5. asynchat 模块 o 7.6. urllib 模块 o 7.7. urlparse 模块 o 7.8. cookie 模块 o 7.9. robotparser 模块 o 7.10. ftplib 模块 o 7.11. gopherlib 模块 o 7.12. httplib 模块 o 7.13. poplib 模块 o 7.14. imaplib 模块 o 7.15. smtplib 模块 o 7.16. telnetlib 模块 o 7.17. nntplib 模块 o 7.18. SocketServer 模块 o 7.19. BaseHTTPServer 模块 o 7.20. SimpleHTTPServer 模块 o 7.21. CGIHTTPServer 模块 o 7.22. cgi 模块 o 7.23. webbrowser 模块

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值