参考链接: fnmatch – Python中的Unix文件名模式匹配

周末研究Robot Framework的源码来提高和保持python的编码能力。 在分析源码文件 pythonpathsetter.py的时候遇到了fnmatch模块,由于之前一般都是采用re模块来进行模式匹配,查了一下Python manual还是收获了欣喜,原来在某些场景下使用re不一定是最好的选择。  从名字来看,不难猜出其用途:File Name Match。

这个模块用于支持Unix B-Shell风格的通配符,再具体点想想make支持三各通配符:“*”,“?”和“[...]”,具体的含义如下:

Pattern  Meaning  *  matches everything  ?  matches any single character  [seq]  matches any character in seq  [!seq]  matches any character not in seq

下面言归正传:

import fnmatch

fnmatch.fnmatch(filename, pattern)

Test whether the filename string matches the pattern string, returning True or False. If the operating system is case-insensitive, then both parameters will be normalized to all lower- or upper-case before the comparison is performed.

这个函数的作用是测试filename是否符合pattern。函数的返回值是True或者False。有些操作系统的文件名是大小敏感的,有些则不然,因此这个函数对传入的两个参数都会被统一化处理。

常用的场景如下:

import fnmatch

import os

for file in os.listdir('.'):

if fnmatch.fnmatch(file, '*.txt'):

print file

除了最常用的fnmatch.fnmatch()函数以外,还有以下几个常用的函数:

fnmatch.fnmatchcase(filename, pattern) Test whether filename matches pattern, returning True or False; the comparison is case-sensitive.

fnmatch.filter(names, pattern) Return the subset of the list of names that match pattern. It is the same as [n for n in names if fnmatch(n, pattern)], but implemented more efficiently. New in version 2.2.

fnmatch.translate(pattern) Return the shell-style pattern converted to a regular expression.Be aware there is no way to quote meta-characters.

Example: >>> import fnmatch, re >>> >>> regex = fnmatch.translate('*.txt') >>> regex '.*\\.txt$' >>> reobj = re.compile(regex) >>> reobj.match('foobar.txt') <_sre.SRE_Match object at 0x...>

[转载] Python: fnmatch模块 (Unix B-Shell通配符的文件名匹配)相关推荐

  1. [转载] Python—urllib模块

    参考链接: Python Urllib模块 urllib模块提供的上层接口使用户能够像读取本地文件一样读取WWW或FTP上的数据,使用起来比C++.C#等编程语言更加方便. 常用的方法如下: 1.ur ...

  2. [转载] Python学习笔记——运维和Shell

    参考链接: 在C / C++,Python,PHP和Java中交换两个变量 目录 什么是运维 运维第一工具-shell编程 shell历史 执行脚本 基本语法 Shell脚本语法 条件测试:test ...

  3. [转载] python 第三方模块

    参考链接: Python | Django-allauth设置和配置 Chardet,字符编码探测器,可以自动检测文本.网页.xml的编码. colorama,主要用来给文本添加各种颜色,并且非常简单 ...

  4. python commands_[转载]python - Commands模块 - 使用方法

    要获得shell命令的输出只需要`cmd`就可以了, 需要得到命令执行的状态则需要判断$?的值, 在Python中有一个模块commands也很容易做到以上的效果. 看一下三个函数: 1). comm ...

  5. python模块的发布_(转载)Python中模块的发布与安装

    模块(Module) Python中有一个概念叫做模块(module),这个和C语言中的头文件以及Java中的包很类似,比如在Python中要调用sqrt函数,必须用import关键字引入math这个 ...

  6. python中weekday_[转载] Python日历模块| 使用示例的weekday()方法

    参考链接: Python中的日历函数 2(monthrange(),prcal(),weekday()-) Python calendar.weekday()方法 (Python calendar.w ...

  7. [转载] Python Pandas 转换unix时间戳

    参考链接: Python | Pandas处理日期和时间 使用pandas自带的pd.to_datetime把 unix 时间戳转为时间时默认是转换为 GMT标准时间 北京时间比这个时间还要加 8个小 ...

  8. [转载] Python: struct 模块之字节对齐问题

    参考链接: Python中的struct模块 P y t h o n Python Python 在二进制写文件时,可以用 s t r u c t struct struct 模块将数据捆绑成结构体转 ...

  9. [转载] Python日历模块| 使用示例的weekday()方法

    参考链接: Python中的日历函数 2(monthrange(),prcal(),weekday()-) Python calendar.weekday()方法 (Python calendar.w ...

最新文章

  1. c++ memset 语言_自学C语言不知道从哪儿下手?学习框架都帮你列好了!
  2. ASP.NET MVC:Cookie 的过期时间在服务器端是获取不到的
  3. C++ 有符号整数和无符号整数修饰符之间的差别
  4. [Leetcode] Reverse Integer
  5. J2EE 第二阶段项目之编写代码(六)
  6. delphi listview 添加数据 慢_Delphi 二十四岁, Delphi 10.3.1 发布
  7. linux lockf文件锁存在,进程停止,Linux文件锁学习-flock, lockf, fcntl
  8. CentOS下MySQL主从同步配置
  9. 对一个正整数n,求出n!中末尾0的个数。
  10. Jenkins在Linux环境安装
  11. powerdesigner简单使用
  12. GIS案例练习-----------第二天
  13. 帆软 填报上传和下载
  14. 纯JavaScript实现鼠标拖尾效果
  15. RPG游戏《黑暗之光》流程介绍与代码分析之(九):技能系统的实现
  16. 快速获取今天是星期几
  17. iOS开发——扫二维码下载APP
  18. OOP-面向对象程序设计
  19. 微信mars学习笔记
  20. Android 详解使用 Zxing实现前置摄像头扫描二维码、生成二维码

热门文章

  1. matlab ext2int,MATLAB程序解读。急急急急急!!!!!!!!!!!
  2. shell解析html文件,[Shell] Shell 生成 HTML脚本,可显示表格
  3. 计算机类专业综合理论模拟试卷4,计算机类专业理论综合考试试题
  4. 数据结构—堆栈(思维导图)
  5. 2018ACM-ICPC南京赛区网络赛: J. Sum(积性函数前缀和)
  6. VK Cup 2018 Round 2: B. Three-level Laser(二分)
  7. 运算优先级、类型转换
  8. bzoj 2733: [HNOI2012]永无乡(线段树启发式合并)
  9. matlab 纹理映射
  10. 吴恩达神经网络和深度学习-学习笔记-45-完全版YOLO算法