liujunjun
一、使用fnmatch找到特定文件
1.建立多个文本,模拟实验内容。

touch {a..z}.txt

2.查找本文件夹中所有以”.txt”结尾的文件并以列表的形式输出。

In [1]: import os                                                                                                                                                       

In [2]: [item for item in os.listdir(\'.\') if item.endswith(\'.txt\')]                                                                                                     
Out[2]: 
[\'data1.txt\',
 \'data2.txt\',
 \'a.txt\',
 \'b.txt\',
 \'c.txt\',
 \'d.txt\',
 \'e.txt\',
 \'f.txt\',
 \'g.txt\',
 \'h.txt\',
 \'i.txt\',
 \'j.txt\',
 \'k.txt\',
 \'l.txt\',
 \'m.txt\',
 \'n.txt\',
 \'o.txt\',
 \'p.txt\',
 \'q.txt\',
 \'r.txt\',
 \'s.txt\',
 \'t.txt\',
 \'u.txt\',
 \'v.txt\',
 \'w.txt\',
 \'x.txt\',
 \'y.txt\',
 \'z.txt\']

大部分情况下,使用字符串匹配查找特定的文件就能够满足需求,如果需要更佳灵活的字符串匹配,可以使用标准库fnmatch库,这个库专门用来进行文件名匹配,支持使用通配符进行字符串匹配。fnmatch支持的通配符如下所示。

* 匹配任何数量的字符
? 匹配单个字符
[seq] 匹配了seq中的字符
[!seq] 匹配除了seq以外的任何字符

fnmatch这个库比较简单,只有4个函数,分别是fnmatch、fnmatchcase、filter和translate。其中最常用的是fnmatch函数。各个函数的作用如下:

fnmatch 判断文件名是否符合特定的模式;
fnmatchcase 判断文件名是否符合特定的模式,不区分大小写;
filter 返回输入列表中,符合特定模式的文件名列表;
translate 将通配符模式转换成正则表达式。

3.创建四个文本,模拟实验环境

touch {a..b}1.txt {c..d}2.jpg

在Python代码中,使用fnmatch函数对当前目录下的4个文件进行匹配性测试,如下所示:

In [3]: import os                                                                                                                                                       

In [4]:  import fnmatch                                                                                                                                                 

In [5]: os.listdir(\'.\')                                                                                                                                                 
Out[5]: 
[\'.bash_logout\',
 \'.bash_profile\',
 \'.bashrc\',
 \'.cshrc\',
 \'.tcshrc\',
 \'anaconda-ks.cfg\',
 \'.bash_history\',
 \'.pki\',
 \'v3.0.6.zip\',
 \'easy-rsa\',
 \'.rnd\',
 \'.pip\',
 \'.cache\',
 \'.ipython\',
 \'data\',
 \'data1.txt\',
 \'data2.txt\',
 \'a.txt\',
 \'b.txt\',
 \'c.txt\',
 \'d.txt\',
 \'e.txt\',
 \'f.txt\',
 \'g.txt\',
 \'h.txt\',
 \'i.txt\',
 \'j.txt\',
 \'k.txt\',
 \'l.txt\',
 \'m.txt\',
 \'n.txt\',
 \'o.txt\',
 \'p.txt\',
 \'q.txt\',
 \'r.txt\',
 \'s.txt\',
 \'t.txt\',
 \'u.txt\',
 \'v.txt\',
 \'w.txt\',
 \'x.txt\',
 \'y.txt\',
 \'z.txt\']

In [6]:                               

 

 

fnmatchcase函数与fnmatch函数几乎一样,只是在匹配文件名时会忽略文件名中的字母大小写。filter函数与fnmatch函数比较类似,区别在于fnmatch每次对一个文件名进行匹配判断,文件名模式为第二个参数,然后以列表的形式返回输入列表中所有符合模式的文件名,如下所示:

In [1]: import os
In [2]: import fnmatch
In [3]: names = os.listdir(‘.’)
In [4]: names
Out[4]: [‘a1.txt’, ‘b1.txt’, ‘c2.jpg’, ‘d2.jpg’]
In [5]: fnmatch.filter(names,”[a-c]?.txt”)

 

Out[5]: [‘a1.txt’, ‘b1.txt’]

In [6]: fnmatch.filter(names,”[!a-c]*”)

Out[6]: [‘d2.jpg’]

二、使用glob找到特定的文件

glob的作用相当于os.listdir加上fnmatch。使用glob后,不需要调用os.listdir获取文件列表,直接通过模式匹配即可,如下所示:

In [1]: import glob
In [2]: glob.glob(‘*.txt’)
Out[2]: [‘a1.txt’, ‘b1.txt’]
In [3]: glob.glob(‘[a-c]?.jpg’)
Out[3]: [‘c2.jpg’]
In [4]: glob.glob(‘[!a-c]?.jpg’)

 

Out[4]: [‘d2.jpg’]

 

分类:

技术点:

相关文章:

  • 2021-12-23
  • 2021-12-23
  • 2021-08-27
  • 2021-11-20
  • 2021-04-24
  • 2021-09-01
  • 2021-12-14
  • 2021-12-23
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-12-15
  • 2021-07-12
  • 2021-12-23
  • 2022-01-12
相关资源
相似解决方案