python 文件操作新姿势 pathlib模块的详细使用

简介: 相比常用的 os.path而言,pathlib 对于目录路径的操作更简介也更贴近 Pythonic。但是它不单纯是为了简化操作,还有更大的用途。pathlib 是Python内置库,Python 文档给它的定义是:The pathlib module – object-oriented filesystem paths(面向对象的文件系统路径)。pathlib 提供表示文件系统路径的类,其语义适用于不同的操作系统。

相比常用的 os.path而言,pathlib 对于目录路径的操作更简介也更贴近 Pythonic。但是它不单纯是为了简化操作,还有更大的用途。

pathlib 是Python内置库,Python 文档给它的定义是:The pathlib module – object-oriented filesystem paths(面向对象的文件系统路径)。pathlib 提供表示文件系统路径的类,其语义适用于不同的操作系统。

20200813205955716.png

更多详细的内容可以参考官方文档:https://docs.python.org/3/library/pathlib.html#methods


一、pathlib模块下 Path 类的基本使用


frompathlibimportPathpath=r'D:\python\pycharm2020\program\pathlib模块的基本使用.py'p=Path(path)
print(p.name)       # 获取文件名print(p.stem)       # 获取文件名除后缀的部分print(p.suffix)     # 获取文件后缀print(p.parent)     # 相当于dirnameprint(p.parent.parent.parent)
print(p.parents)    # 返回一个iterable 包含所有父目录foriinp.parents:
print(i)
print(p.parts)      # 将路径通过分隔符分割成一个元组


运行结果如下:


pathlib模块的基本使用.pypathlib模块的基本使用.pyD:\python\pycharm2020\programD:\python<WindowsPath.parents>D:\python\pycharm2020\programD:\python\pycharm2020D:\pythonD:\
('D:\\', 'python', 'pycharm2020', 'program', 'pathlib模块的基本使用.py')


  • Path.cwd():Return a new path object representing the current directory
  • Path.home():Return a new path object representing the user’s home directory
  • Path.expanduser():Return a new path with expanded ~ and ~user constructs


frompathlibimportPathpath_1=Path.cwd()       # 获取当前文件路径path_2=Path.home()
p1=Path('~/pathlib模块的基本使用.py')
print(path_1)
print(path_2)
print(p1.expanduser())


运行结果如下:


D:\python\pycharm2020\programC:\Users\AdministratorC:\Users\Administrator\pathlib模块的基本使用.py


Path.stat():Return a os.stat_result object containing information about this path


frompathlibimportPathimportdatetimep=Path('pathlib模块的基本使用.py')
print(p.stat())            # 获取文件详细信息print(p.stat().st_size)    # 文件的字节大小print(p.stat().st_ctime)   # 文件创建时间print(p.stat().st_mtime)   # 上次修改文件的时间creat_time=datetime.datetime.fromtimestamp(p.stat().st_ctime)
st_mtime=datetime.datetime.fromtimestamp(p.stat().st_mtime)
print(f'该文件创建时间:{creat_time}')
print(f'上次修改该文件的时间:{st_mtime}')


运行结果如下:


os.stat_result(st_mode=33206, st_ino=3659174698076635, st_dev=3730828260, st_nlink=1, st_uid=0, st_gid=0, st_size=543, st_atime=1597366826, st_mtime=1597366826, st_ctime=1597320585)
5431597320585.76574751597366826.9711637该文件创建时间:2020-08-1320:09:45.765748上次修改该文件的时间:2020-08-1409:00:26.971164


从不同.stat().st_属性 返回的时间戳表示自1970年1月1日以来的秒数,可以用datetime.fromtimestamp将时间戳转换为有用的时间格式。


frompathlibimportPathp1=Path('pathlib模块的基本使用.py')          # 文件p2=Path(r'D:\python\pycharm2020\program')   # 文件夹 absolute_path=p1.resolve()
print(absolute_path)
print(Path('.').exists())
print(p1.exists(), p2.exists())
print(p1.is_file(), p2.is_file())
print(p1.is_dir(), p2.is_dir())
print(Path('/python').exists())
print(Path('non_existent_file').exists())


运行结果如下:


D:\python\pycharm2020\program\pathlib模块的基本使用.pyTrueTrueTrueTrueFalseFalseTrueTrueFalse


  • Path.iterdir():When the path points to a directory,yield path objects of the directory contents


frompathlibimportPathp=Path('/python')
forchildinp.iterdir():
print(child)


运行结果如下:


\python\Anaconda\python\EVCapture\python\Evernote_6.21.3.2048.exe\python\Notepad++\python\pycharm-community-2020.1.3.exe\python\pycharm2020\python\pyecharts-assets-master\python\pyecharts-gallery-master\python\Sublimetext3


  • Path.glob(pattern):Glob the given relative pattern in the directory represented by this path, yielding all matching files (of any kind),The “**” pattern means “this directory and all subdirectories, recursively”. In other words, it enables recursive globbing.


  • Note:Using the “**” pattern in large directory trees may consume an inordinate amount of time


递归遍历该目录下所有文件,获取所有符合pattern的文件,返回一个generator。


获取该文件目录下所有.py文件


frompathlibimportPathpath=r'D:\python\pycharm2020\program'p=Path(path)
file_name=p.glob('**/*.py')
print(type(file_name))   # <class 'generator'>foriinfile_name:
print(i)


获取该文件目录下所有.jpg图片


frompathlibimportPathpath=r'D:\python\pycharm2020\program'p=Path(path)
file_name=p.glob('**/*.jpg')
print(type(file_name))   # <class 'generator'>foriinfile_name:
print(i)


获取给定目录下所有.txt文件、.jpg图片和.py文件


frompathlibimportPathdefget_files(patterns, path):
all_files= []
p=Path(path)
foriteminpatterns:
file_name=p.rglob(f'**/*{item}')
all_files.extend(file_name)
returnall_filespath=input('>>>请输入文件路径:')
results=get_files(['.txt', '.jpg', '.py'], path)
print(results)
forfileinresults:
print(file)


Path.mkdir(mode=0o777, parents=False, exist_ok=False)


  • Create a new directory at this given path. If mode is given, it is combined with the process’ umask value to determine the file mode and access flags. If the path already exists, FileExistsError is raised.
  • If parents is true, any missing parents of this path are created as needed; they are created with the default permissions without taking mode into account (mimicking the POSIX mkdir -p command).
  • If parents is false (the default), a missing parent raises FileNotFoundError.
  • If exist_ok is false (the default), FileExistsError is raised if the target directory already exists.
  • If exist_ok is true, FileExistsError exceptions will be ignored (same behavior as the POSIX mkdir -p command), but only if the last path component is not an existing non-directory file.


Changed in version 3.5: The exist_ok parameter was added.

Path.rmdir():Remove this directory. The directory must be empty.


frompathlibimportPathp=Path(r'D:\python\pycharm2020\program\test')
p.mkdir()
p.rmdir()


frompathlibimportPathp=Path(r'D:\python\test1\test2\test3')
p.mkdir(parents=True)  # If parents is true, any missing parents of this path are created as neededp.rmdir()    # 删除的是test3文件夹


frompathlibimportPathp=Path(r'D:\python\test1\test2\test3')
p.mkdir(exist_ok=True)


  • Path.unlink(missing_ok=False):Remove this file or symbolic link. If the path points to a directory, use Path.rmdir() instead. If missing_ok is false (the default), FileNotFoundError is raised if the path does not exist. If missing_ok is true, FileNotFoundError exceptions will be ignored. Changed in version 3.8:The missing_ok parameter was added.
  • Path.rename(target):Rename this file or directory to the given target, and return a new Path instance pointing to target. On Unix, if target exists and is a file, it will be replaced silently if the user has permission. target can be either a string or another path object.
  • Path.open(mode=‘r’, buffering=-1, encoding=None, errors=None, newline=None):Open the file pointed to by the path, like the built-in open() function does.


frompathlibimportPathp=Path('foo.txt')
p.open(mode='w').write('some text')
target=Path('new_foo.txt')
p.rename(target)
content=target.open(mode='r').read()
print(content)
target.unlink()


二、与os模块用法的对比



pathlib2.png


三、实战案例


对于多层文件夹的读取,用os模块只能一层一层读取出文件,要写多个for循环,效率不高,这时我们可以用 Path.glob(**/*) 大法,下面以一个实际案例来体验它的强大。


用于测试的文件夹如下:


pathlib3.gif


md文件中数据如下:


pathlib4.png


需要实现将该目录下所有 md 文件的数据提取出来,并进行清洗,然后写入 csv 文件中。

# -*- coding: UTF-8 -*-"""@File    :pathlib读取多层目录数据.py@Author  :叶庭云@CSDN    :https://yetingyun.blog.csdn.net/"""frompathlibimportPathimportreimportpandasaspd# 传入路径p=Path(r'.\微博热搜数据\热搜数据/')
# 得到该文件目录下所有 .md文件file_list=list(p.glob('**/*.md'))
print(f'读取md文件数量:{len(file_list)}')
foriteminfile_list:
print(item)
# 每天有两条热搜汇总  11点  23点  会有重合数据  去重filelist=list(filter(lambdax: str(x).find('23点') >=0, file_list))
sum_list= []
i=0forfileinfilelist:
# 遍历出每一个md文件   读取数据withfile.open(encoding='utf-8') asf:
lines=f.readlines()
lines= [i.strip() foriinlines]   # 去除空字符data=list(filter(None, lines))     # 去除掉列表中的空子串data=data[1:101]
con=data[::2]     # 热搜内容rank=data[1::2]   # 热度date=re.findall('年(.+)2', str(file)) *len(con)
forminrange(len(con)):
con[m] =con[m].split('、')[-1]   # 字符串操作forninrange(len(rank)):
rank[n] =re.findall(r'\d+', rank[n])[0]
con_dic= {'日期': date, '热搜内容': con, '热度': rank}
df=pd.DataFrame(con_dic)
ifi==0:
df.to_csv('weibo1.csv', mode='a+', index=False, header=True)
else:
df.to_csv('weibo1.csv', mode='a+', index=False, header=False)
# 每个md文件中有50条数据i+=50print('共{}条数据写入csv'.format(i))


运行效果如下:

20201104222951120.gif

pathlib6.gif

成功将该目录下所有 md 文件的数据提取出来,并进行清洗,然后写入了 csv 文件中。

目录
相关文章
|
7月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
868 7
|
7月前
|
监控 安全 程序员
Python日志模块配置:从print到logging的优雅升级指南
从 `print` 到 `logging` 是 Python 开发的必经之路。`print` 调试简单却难维护,日志混乱、无法分级、缺乏上下文;而 `logging` 支持级别控制、多输出、结构化记录,助力项目可维护性升级。本文详解痛点、优势、迁移方案与最佳实践,助你构建专业日志系统,让程序“有记忆”。
602 0
|
7月前
|
JSON 算法 API
Python中的json模块:从基础到进阶的实用指南
本文深入解析Python内置json模块的使用,涵盖序列化与反序列化核心函数、参数配置、中文处理、自定义对象转换及异常处理,并介绍性能优化与第三方库扩展,助你高效实现JSON数据交互。(238字)
608 4
|
8月前
|
安全 Python
告别 os.path 的繁琐:拥抱 Python 的 pathlib
告别 os.path 的繁琐:拥抱 Python 的 pathlib
551 6
|
8月前
|
安全 大数据 程序员
Python operator模块的methodcaller:一行代码搞定对象方法调用的黑科技
`operator.methodcaller`是Python中处理对象方法调用的高效工具,替代冗长Lambda,提升代码可读性与性能。适用于数据过滤、排序、转换等场景,支持参数传递与链式调用,是函数式编程的隐藏利器。
250 4
|
7月前
|
Java 调度 数据库
Python threading模块:多线程编程的实战指南
本文深入讲解Python多线程编程,涵盖threading模块的核心用法:线程创建、生命周期、同步机制(锁、信号量、条件变量)、线程通信(队列)、守护线程与线程池应用。结合实战案例,如多线程下载器,帮助开发者提升程序并发性能,适用于I/O密集型任务处理。
697 0
|
7月前
|
XML JSON 数据处理
超越JSON:Python结构化数据处理模块全解析
本文深入解析Python中12个核心数据处理模块,涵盖csv、pandas、pickle、shelve、struct、configparser、xml、numpy、array、sqlite3和msgpack,覆盖表格处理、序列化、配置管理、科学计算等六大场景,结合真实案例与决策树,助你高效应对各类数据挑战。(238字)
977 0
|
8月前
|
存储 数据库 开发者
Python SQLite模块:轻量级数据库的实战指南
本文深入讲解Python内置sqlite3模块的实战应用,涵盖数据库连接、CRUD操作、事务管理、性能优化及高级特性,结合完整案例,助你快速掌握SQLite在小型项目中的高效使用,是Python开发者必备的轻量级数据库指南。
706 0
|
9月前
|
存储 安全 数据处理
Python 内置模块 collections 详解
`collections` 是 Python 内置模块,提供多种高效数据类型,如 `namedtuple`、`deque`、`Counter` 等,帮助开发者优化数据处理流程,提升代码可读性与性能,适用于复杂数据结构管理与高效操作场景。
552 0
|
10月前
|
数据安全/隐私保护 Python
抖音私信脚本app,协议私信群发工具,抖音python私信模块
这个实现包含三个主要模块:抖音私信核心功能类、辅助工具类和主程序入口。核心功能包括登录

推荐镜像

更多