python 的标准库模块glob使用教程,主要为glob.glob()使用与glob.iglob()使用

article/2025/10/27 23:24:32
欢迎大家关注笔者,你的关注是我持续更博的最大动力


原创文章,转载告知,盗版必究


python 的标准库模块glob使用教程,主要为glob.glob函数使用与glob.iglob函数使用

文章目录:

  • 1 glob模块介绍
  • 2 glob模块的具体使用
    • 2.1 查看glob模块有哪些方法属性
    • 2.2 glob.glob(pathname, *, recursive=False)函数的使用
      • 2.2.1 函数glob.glob()定义:
      • 2.2.2 glob.glob()函数的参数和返回值
      • 2.2.3 glob.glob()函数使用实例
    • 2.3 glob.iglob(pathname, recursive=False)函数的使用
      • 2.3.1 glob.iglob()函数的定义
      • 2.3.2 glob.iglob()函数的参数
      • 2.3.3 glob.iglob()函数的使用实例
    • 2.4 其他通配符`*、?、[]`实例


1 glob模块介绍

glob是python的标准库模块,只要安装python就可以使用该模块。glob模块主要用来查找目录文件,可以使用*、?、[]这三种通配符对路径中的文件进行匹配。

  • *:代表0个或多个字符
  • ?:代表一个字符
  • []:匹配指定范围内的字符,如[0-9]匹配数字

Unix样式路径名模式扩展

  • glob模块英文文档:https://docs.python.org/3/library/glob.html

2 glob模块的具体使用

2.1 查看glob模块有哪些方法属性

>>> dir(glob)
['__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', 
'__name__', '__package__', '__spec__', '_glob0', '_glob1', '_glob2', '_iglob', 
'_ishidden', '_isrecursive', '_iterdir', '_rlistdir', 'escape', 'fnmatch', 
'glob', 'glob0', 'glob1', 'has_magic', 'iglob', 'magic_check', 
'magic_check_bytes', 'os', 're']
>>>

glob模块常用的两个方法有:glob.glob() 和 glob.iglob,下面详细介绍

2.2 glob.glob(pathname, *, recursive=False)函数的使用

2.2.1 函数glob.glob()定义:

def glob(pathname, *, recursive=False):"""Return a list of paths matching a pathname pattern.The pattern may contain simple shell-style wildcards a lafnmatch. However, unlike fnmatch, filenames starting with adot are special cases that are not matched by '*' and '?'patterns.If recursive is true, the pattern '**' will match any files andzero or more directories and subdirectories."""return list(iglob(pathname, recursive=recursive))def iglob(pathname, *, recursive=False):"""Return an iterator which yields the paths matching a pathname pattern.The pattern may contain simple shell-style wildcards a lafnmatch. However, unlike fnmatch, filenames starting with adot are special cases that are not matched by '*' and '?'patterns.If recursive is true, the pattern '**' will match any files andzero or more directories and subdirectories."""it = _iglob(pathname, recursive, False)if recursive and _isrecursive(pathname):s = next(it)  # skip empty stringassert not sreturn it

2.2.2 glob.glob()函数的参数和返回值

  • def glob(pathname, *, recursive=False):
    • pathname:该参数是要匹配的路径
    • recursive:如果是true就会递归的去匹配符合的文件路径,默认是False
  • 返回匹配到的路径列表

2.2.3 glob.glob()函数使用实例

先给出测试使用的目录结构:

test_dir/
├── a1.txt
├── a2.txt
├── a3.py
├── sub_dir1
│   ├── b1.txt
│   ├── b2.py
│   └── b3.py
└── sub_dir2├── c1.txt├── c2.py└── c3.txt

1、返回目录的路径列表

>>> path_list1 = glob.glob('./test_dir/')
>>> path_list
['./test_dir/']

2、匹配'./test_dir/*路径下的所有目录和文件,并返回路径列表

>>> path_list2 = glob.glob('./test_dir/*')
>>> path_list2
['./test_dir/a3.py', './test_dir/a2.txt', './test_dir/sub_dir1', './test_dir/sub_dir2', './test_dir/a1.txt']

3、匹配./test_dir/路径下含有的所有.py文件不递归

>>> path_list3 = glob.glob('./test_dir/*.py')
>>> path_list3
['./test_dir/a3.py']
>>> path_list4 = glob.glob('./test_dir/*/*.py')
>>> path_list4
['./test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

4、递归的匹配./test_dir/**路径下的所有目录和文件,并返回路径列表

>>> path_list5 = glob.glob('./test_dir/**', recursive=True)
>>> path_list5
['./test_dir/', './test_dir/a3.py', './test_dir/a2.txt', './test_dir/sub_dir1', './test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir1/b1.txt', './test_dir/sub_dir2', './test_dir/sub_dir2/c3.txt', './test_dir/sub_dir2/c1.txt', './test_dir/sub_dir2/c2.py', './test_dir/a1.txt']
>>> path_list6 = glob.glob('./test_dir/**/*.py', recursive=True)
>>> path_list6
['./test_dir/a3.py', './test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

注意:

如果要对某个路径下进行递归,一定要在后面加两个*

>>> path_list = glob.glob('./test_dir/', recursive=True)
>>> path_list
['./test_dir/']

2.3 glob.iglob(pathname, recursive=False)函数的使用

2.3.1 glob.iglob()函数的定义

def iglob(pathname, *, recursive=False):"""Return an iterator which yields the paths matching a pathname pattern.The pattern may contain simple shell-style wildcards a lafnmatch. However, unlike fnmatch, filenames starting with adot are special cases that are not matched by '*' and '?'patterns.If recursive is true, the pattern '**' will match any files andzero or more directories and subdirectories."""it = _iglob(pathname, recursive, False)if recursive and _isrecursive(pathname):s = next(it)  # skip empty stringassert not sreturn it

2.3.2 glob.iglob()函数的参数

  • glob.iglob参数glob.glob()一样
  • def iglob(pathname, *, recursive=False):
    • pathname:该参数是要匹配的路径
    • recursive:如果是true就会递归的去匹配符合的文件路径,默认是False
  • 返回一个迭代器,遍历该迭代器的结果与使用相同参数调用glob()的返回结果一致

2.3.3 glob.iglob()函数的使用实例

先给出测试使用的目录结构:

test_dir/
├── a1.txt
├── a2.txt
├── a3.py
├── sub_dir1
│   ├── b1.txt
│   ├── b2.py
│   └── b3.py
└── sub_dir2├── c1.txt├── c2.py└── c3.txt

正常glob.glob()返回路径列表

>>> path_list4 = glob.glob('./test_dir/*/*.py')
>>> path_list4
['./test_dir/sub_dir1/b2.py', './test_dir/sub_dir1/b3.py', './test_dir/sub_dir2/c2.py']

现在,使用:glob.iglob()

>>> file_path_iter = glob.iglob('./test_dir/*')
>>> print(type(file))
<class 'generator'>
>>> for file_path in file_path_iter:
...     print(file_path)
...
./test_dir/a3.py
./test_dir/a2.txt
./test_dir/sub_dir1
./test_dir/sub_dir2
./test_dir/a1.txt
>>>

2.4 其他通配符*、?、[]实例

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
>>> glob.glob('**/*.txt', recursive=True)
['2.txt', 'sub/3.txt']
>>> glob.glob('./**/', recursive=True)
['./', './sub/']

在这里插入图片描述




在这里插入图片描述
♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠ ⊕ ♠


http://chatgpt.dhexx.cn/article/AtOE9Oh9.shtml

相关文章

glob.glob()

glob.glob()函数 glob.glob()含有的内容非常少&#xff0c;其功能是查找符合特定规则的文件路径。例如&#xff08;F:/Python/Hello&#xff09;。 功能类似于Windows的文件搜索。 查找文件只用到三个匹配符&#xff1a;" “,”?", “[]”。 " "&#x…

glob函数

查找符合自己要求的文件 glob.glob() 返回一个列表&#xff0c;列表里的元素是符合要求的文件名&#xff08;字符串&#xff09; 三个匹配符&#xff1a; ”*”匹配任意0个或多个字符 ”?”匹配任意单个字符 ”[ ]”匹配指定范围内的字符 例子1&#xff1a; glob.glob(&q…

python 中的 glob.glob()

1. glob方法&#xff1a; glob模块的主要方法就是glob,该方法返回所有匹配的文件路径列表&#xff08;list&#xff09;&#xff1b; 该方法需要一个参数用来指定匹配的路径字符串&#xff08;字符串可以为绝对路径也可以为相对路径&#xff09;&#xff0c;其返回的文件名只…

python glob函数_Python glob()函数

Python glob()函数&#xff0c;是种文件通配符&#xff0c;非常常用。glob模块提供了函数用于从目录通配符搜索中生成文件列表: Python glob()函数用法 glob()函数可以查找符合自己要求的文件&#xff0c; 支持通配符操作*,**,?,[]这四个通配符&#xff0c; *代表0个或多个字符…

python glob.glob使用

使用了下thrift&#xff0c;出现了一些路径问题。原来是glob.glob函数捣的鬼。所以扒了下英文文档&#xff0c;有些收获。下面分享。 函数功能&#xff1a;匹配所有的符合条件的文件&#xff0c;并将其以list的形式返回。 一.glob.glob的介绍和一些实现细节 1.glob.glob函数的…

glob.glob() 函数

glob模块是最简单的模块之一&#xff0c;内容非常少。用它可以查找符合特定规则的文件路径名。跟使用windows下的文件搜索差不多。查找文件只用到三个匹配符&#xff1a;””, “?”, “[]”。””匹配0个或多个字符&#xff1b;”?”匹配单个字符&#xff1b;”[]”匹配指定…

python glob.glob()

glob是python的一个标准库函数&#xff0c;它的作用类似于shell的find&#xff0c;但是只能返回一级目录的查找结果&#xff0c;支持通配符来查找某个目录的文件&#xff0c;找到的文件列表以list格式返回&#xff0c;如果目录不存在或者找到结果为空&#xff0c;则返回一个空列…

glob.glob()函数

glob.glob()函数 glob.glob(pathname, *, recursiveFalse) 功能&#xff1a;返回一个某一种文件夹下面的某一类型文件路径列表 pathname:文件路径 例如&#xff1a; 返回某一文件下面的pdf文件 import glob f glob.glob(H:\\paper_of_remote_sensing\\paper_of_lidar\\*.…

Linux杂谈之Glob文件名模式匹配

一 Glob ① 初识 glob 最早是出现在类Unix系统的命令行中,是用来匹配文件路径的1&#xff09;在最新的 CentOS 7 中已经删除了 glob 的相关描述文档2&#xff09;删除的原因由于 glob 已经整合到了 shell 之中,然后就有了 shell 通配符常见&#xff1a; 涉及glob的相关命令…

Python 中glob.glob()、glob.iglob()的使用

Python 中glob.glob()的使用 glob.glob(path)的功能&#xff1a; 返回符合path格式的所有文件的路径&#xff0c;以list存储返回。 glob.iglob(path)的功能&#xff1a; 返回符合path格式的一个文件的路径。 path的表示方法&#xff1a; 利用匹配符&#xff1a;"* "…

SSM项目小例子,SSM整合图文详细教程

SSM项目小例子 今天来搭建一个SSM项目的小例子简单练一练&#xff0c;那项目模板还是我们那个模板&#xff0c;就是我们在JavaWeb最后的小例子&#xff0c;那到SSM中我们如何实现&#xff0c;后面我们再看看springboot中如何实现 javaweb中项目例子&#xff1a;https://blog.cs…

SSM整合之登录注册

一、概述 本文以一个登录注册的小功能作为示例&#xff0c;对SSM框架做一个整合。 二、SSM整合 SSM框架是指Spring、SpringMVC和Mybatis&#xff0c;SpringMVC是包含在Spring中的&#xff0c;因此SSM框架整合核心是将Mybatis整合到Spring中。 2.1 DAO 1. 创建如下的数据库表格…

遇见狂神说SSM整合项目梳理详解

在跟B站的狂神学习的时候有一模块是SSM整合的一个小项目&#xff0c;虽然spring、springmvc、mybatis我都学过&#xff0c;但是整合到一起还是思路不清晰&#xff0c;就很懵&#xff0c;整合的一个小项目&#xff0c;让我知道了什么叫配置地狱&#xff0c;就是熟悉又陌生的感觉…

SSM整合Quartz

Quartz 1.整合1.1 mysql中创建quartz表1.2 添加quartz依赖1.3 添加quartz.properties与数据库关联1.4 编写quartzConfig文件1.4.1 quartz的初始化配置1.4.2 创建job 实例工厂 1.5 编写定时任务1.5.1 静态方式编写定时任务1.5.2 动态方式编写定时任务 2.quartz2.1 quartz概念2.2…

SSM 整合

目录 第六章 SSM 整合导图第一节 Spring 和 Mybatis 整合1、思路2、Mybatis-Spring技术3、总体 SSM 整合所需依赖4、配置数据源①创建 jdbc.properties②加入日志配置文件③创建 Spring 配置文件④创建 junit 测试类 5、配置 SqlSessionFactoryBean①创建 Mybatis 全局配置文件…

SSM整合Vue

昨日知识点总结 今日总结 文章目录 一.昨日知识 1.表单验证2.上传组件3.时间插件4.ElementUI简介 二.今日总结 1.页面静态化 通过html页面&#xff0c;制作展示所有制作添加页面 制作修改页面以及批量删除制作分页制作多条件查询 一、昨日重点复习 1.表单验证 在vue中使用rule…

SSM整合过程梳理

文章目录 前言一.SSM整合流程二.整合配置2.1添加依赖2.2创建项目包结构2.3创建SpringConfig配置类2.4创建JdbcConfig配置类2.5创建MybatisConfig配置类2.6创建jdbc.properties2.7创建SpringMVC配置类2.8创建Web项目入口配置类 三.功能模块3.1创建模型类3.2编写Dao接口3.3编写Se…

ssm整合详解

最近做项目用到了ssm,虽然以前用过ssm但这段时间发现&#xff0c;用过不代表就会了&#xff0c;即使以前用过&#xff0c;但现在要搭一个ssm框架不看教程还是很难&#xff0c;最基本的maven仓库需要哪些坐标都搞不清楚&#xff0c;所以今天打算写篇博客梳理一下。 一.基础梳理…

SSM整合分页插件

目录 一.环境配置 1.分页插件依赖 2.在Mybatis里面配置的内容 二.使用分页插件 1.在ServiceImpl层开启分页插件&#xff08;即查询数据库前开启&#xff09; 2.控制器方法 3.jsp页面进行整理 4.效果 5. 样式 6.常用的数据说明 一.环境配置 1.分页插件依赖 <depend…

SSM整合总结

这几天学完spring&#xff0c;springMVC&#xff0c;mybatis后&#xff0c;这两天试着去整合ssm&#xff0c;整合过程并不顺利&#xff0c;一是基础知识有的忘了&#xff0c;细节不能把握住&#xff0c;造成各种报错.看了各种视频&#xff0c;翻阅各种文章&#xff0c;多多少少…