开眼界!Python遍历文件可以这样做

liftword4个月前 (12-19)技术文章54

来源:【公众号】

Python技术

Python 对于文件夹或者文件的遍历一般有两种操作方法,一种是至二级利用其封装好的 walk 方法操作:


import os
for root,dirs,files in os.walk("/Users/cxhuan/Downloads/globtest/hello"):
    for dir in dirs:
        print(os.path.join(root, dir))
    for file in files:
        print(os.path.join(root, file))

上面代码运行结果如下:

/Users/cxhuan/Downloads/globtest/hello/world
/Users/cxhuan/Downloads/globtest/hello/.DS_Store
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world1.txt
/Users/cxhuan/Downloads/globtest/hello/world/world3.txt
/Users/cxhuan/Downloads/globtest/hello/world/world2.txt

上述程序,将 os.walk 读取到的所有路径 root 、目录名 dirs 与文件名 files ,也就是三个文件数组利用 foreach 循环输出。join方法就是将其路径与目录名或者文件名连接起来,组成一个完整的目录。

另一种是用递归的思路,写成下面的形式:


import os
files = list()
def dirAll(pathname):
    if os.path.exists(pathname):
        filelist = os.listdir(pathname)
        for f in filelist:
            f = os.path.join(pathname, f)
            if os.path.isdir(f):
                dirAll(f)
            else:
                dirname = os.path.dirname(f)
                baseName = os.path.basename(f)
                if dirname.endswith(os.sep):
                    files.append(dirname+baseName)
                else:
                    files.append(dirname+os.sep+baseName)


dirAll("/Users/cxhuan/Downloads/globtest/hello")
for f in files:
    print(f)

运行上面代码,得到的结果和上面一样。

这两种方法都没问题,就是写起来比较麻烦,特别是第二种,一不小心还有可能写出 bug 。

今天我们来介绍第三种方法——利用 glob 模块来遍历文件。

简介

glob 是 python 自带的一个操作文件的模块,以简洁实用著称。由于这个模块的功能比较简单,所以也很容易上手和使用。它主要用来查找符合特定规则的文件路径。使用这个模块来查找文件,只需要用到*?[] 这三个匹配符:

 * : 匹配0个或多个字符;
 ? : 匹配单个字符;
 [] :匹配指定范围内的字符,如:[0-9]匹配数字。

glob.glob 方法

glob.glob 方法主要返回所有匹配的文件路径列表。它只有一个参数 pathname ,定义了文件路径匹配规则,这里可以是绝对路径,也可以是相对路径。

使用 * 匹配

我们可以用 * 匹配零个或者多个字符。

输出目录下的子目录或者文件:


for p1 in glob.glob('/Users/cxhuan/Downloads/globtest/*'):
    print(p1)

运行上面代码,会将 globtest 文件夹下仅有的目录输出出来,输出内容如下:


/Users/cxhuan/Downloads/globtest/hello

我们也可以通过制定层级来遍历文件或者文件夹:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/*/*'):
    print(p)

上面的代码会遍历 globtest 文件夹以及子文件夹,将所有的文件或文件夹路径打印出来:

/Users/cxhuan/Downloads/globtest/hello/world
/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

我们也可以对文件或者文件夹进行过滤:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*3.txt'):
    print(p)

上面代码值匹配 hello 目录下的文件名末尾为 ‘3’ 的 txt 文件,运行结果如下:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt

使用 ? 匹配

我们可以用问号(?)匹配任何单个的字符。


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.txt'):
    print(p)

上面的代码输出 hello 目录下的以 ‘hello’ 开头的 txt 文件,输出结果如下:


/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

使用 [] 匹配

我们可以使用 [] 来匹配一个范围:


for p in glob.glob('/Users/cxhuan/Downloads/globtest/hello/*[0-2].*'):
    print(p)

我们想要得到 hello 目录下的文件名结尾数字的范围为 0到2的文件,运行上面代码,获得的输出为:

/Users/cxhuan/Downloads/globtest/hello/hello2.txt
/Users/cxhuan/Downloads/globtest/hello/hello1.txt

glob.iglob 方法

python 的 glob 方法可以对文件夹下所有文件进行遍历,并返回一个 list 列表。而 iglob 方法一次只获取一个匹配路径。下面是一个简单的例子来说明二者的区别:

p = glob.glob('/Users/cxhuan/Downloads/globtest/hello/hello?.*')
print(p)

print('----------------------')

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*')
print(p)

运行上面代码,结果返回是:


['/Users/cxhuan/Downloads/globtest/hello/hello3.txt', '/Users/cxhuan/Downloads/globtest/hello/hello2.txt', '/Users/cxhuan/Downloads/globtest/hello/hello1.txt']
----------------------
<generator object _iglob at 0x1040d8ac0>

从上面的结果我们可以很容易看到二者的区别,前者返回的是一个列表,后者返回的是一个可迭代对象。

我们针对这个可迭代对象做一下操作看看:

p = glob.iglob('/Users/cxhuan/Downloads/globtest/hello/hello?.*')
print(p.__next__())
print(p.__next__())

运行结果如下:

/Users/cxhuan/Downloads/globtest/hello/hello3.txt
/Users/cxhuan/Downloads/globtest/hello/hello2.txt

我们可以看到,针对这个可迭代对象,我们一次可以获取到一个元素。这样做的好处是节省内存,试想如果一个路径下有大量的文件夹或者文件,我们使用这个迭代对象不用一次性全部获取到内存,而是可以慢慢获取。

总结

今天分享的模块虽然功能简单,但是对于我们遍历文件或者目录来说足够使用了,并且方法简单易懂,值得大家经常使用。如果你觉得今天分享的模块有用,点个“”支持一下吧!

相关文章

Python列表的使用方法 python列表的常用方法

理解 Python 列表从本质上讲,列表是项目的集合,每个项目都由一个索引标识。列表的用途非常广泛,可以存储各种数据类型,包括数字、字符串,甚至其他列表。它们是 Python 的基本数据结构之一,广泛...

Python 中的列表推导式详解 python列表讲解

· 在 Python 中,列表推导式是一种简洁而富有表现力的方式,通过以紧凑的语法指定元素和条件来创建数据结构,如列表、字典和集合,通常使用单行代码,而不是使用传统的循环和附加操作。 在 Python...

Python 遍历指定目录中的内容方法

Python有多种方法可以用于遍历指定目录中的内容,其中一种常用的方法是使用os模块的walk()函数。os.walk()函数可以遍历一个目录以及所有的子目录,并且对于每一个目录,都可以产生一个三元组...

某校教务管理系统post分析,Python实现自动查询成绩并发送短信

前言本人是一名大三大学生,考完试不久,由于自己不知道期末考试什么时候出考试成绩,并且每次查询成绩特别麻烦(首先得登录VPN连接学校内网,然后再登录教务管理系统,再进入查询界面,点击查询成绩等,相信各位...

网络工程师如何使用 Python 批量收集网络设备的 MAC 地址?

在网络管理中,网络工程师经常需要收集和管理大量设备的MAC地址。这些MAC地址对于维护网络设备的安全性和稳定性至关重要。在大型网络环境中,手动收集这些信息既费时又容易出错。因此,利用Python脚本自...

从零开始学习python(5)——分支与循环结构

1. 分支结构Python中的分支结构主要是由if语句实现的,一共有四种。1.1 if语句判断一个条件,如果这个条件成立,就执行其包含的某条语句或某个代码块。语法规则:if condition:sta...