前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python 文件操作新姿势 pathlib模块的详细使用

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

作者头像
叶庭云
发布2022-05-09 08:02:49
5360
发布2022-05-09 08:02:49
举报
文章被收录于专栏:Python进阶之路Python进阶之路

文章目录

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

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

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

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

代码语言:javascript
复制
from pathlib import Path

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

运行结果如下:

代码语言:javascript
复制
pathlib模块的基本使用.py
pathlib模块的基本使用
.py
D:\python\pycharm2020\program
D:\python
<WindowsPath.parents>
D:\python\pycharm2020\program
D:\python\pycharm2020
D:\python
D:\
('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
代码语言:javascript
复制
from pathlib import Path

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

运行结果如下:

代码语言:javascript
复制
D:\python\pycharm2020\program
C:\Users\Administrator
C:\Users\Administrator\pathlib模块的基本使用.py

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

代码语言:javascript
复制
from pathlib import Path
import datetime

p = 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}')

运行结果如下:

代码语言:javascript
复制
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)
543
1597320585.7657475
1597366826.9711637
该文件创建时间:2020-08-13 20:09:45.765748
上次修改该文件的时间:2020-08-14 09:00:26.971164

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

  • Path.exists():Whether the path points to an existing file or directory
  • Path.resolve(strict=False):Make the path absolute,resolving any symlinks. A new path object is returned
代码语言:javascript
复制
from pathlib import Path

p1 = 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())

运行结果如下:

代码语言:javascript
复制
D:\python\pycharm2020\program\pathlib模块的基本使用.py
True
True True
True False
False True
True
False
  • Path.iterdir():When the path points to a directory,yield path objects of the directory contents
代码语言:javascript
复制
from pathlib import Path

p = Path('/python')
for child in p.iterdir():
    print(child)

运行结果如下:

代码语言:javascript
复制
\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\Sublime text 3
  • 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文件

代码语言:javascript
复制
from pathlib import Path

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

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

代码语言:javascript
复制
from pathlib import Path

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

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

代码语言:javascript
复制
from pathlib import Path

def get_files(patterns, path):
    all_files = []
    p = Path(path)
    for item in patterns:
        file_name = p.rglob(f'**/*{item}')
        all_files.extend(file_name)
    return all_files

path = input('>>>请输入文件路径:')
results = get_files(['.txt', '.jpg', '.py'], path)
print(results)
for file in results:
    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.

代码语言:javascript
复制
from pathlib import Path

p = Path(r'D:\python\pycharm2020\program\test')
p.mkdir()
p.rmdir()
代码语言:javascript
复制
from pathlib import Path

p = Path(r'D:\python\test1\test2\test3')
p.mkdir(parents=True)  # If parents is true, any missing parents of this path are created as needed
p.rmdir()    # 删除的是test3文件夹
代码语言:javascript
复制
from pathlib import Path

p = 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.
代码语言:javascript
复制
from pathlib import Path

p = 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模块用法的对比

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

三、实战案例

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

用于测试的文件夹如下:

md文件中数据如下:

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

代码语言:javascript
复制
# -*- coding: UTF-8 -*-
"""
@File    :pathlib读取多层目录数据.py
@Author  :叶庭云
@CSDN    :https://yetingyun.blog.csdn.net/
"""
from pathlib import Path
import re
import pandas as pd


# 传入路径
p = Path(r'.\微博热搜数据\热搜数据/')
# 得到该文件目录下所有 .md文件
file_list = list(p.glob('**/*.md'))
print(f'读取md文件数量:{len(file_list)}')
for item in file_list:
	print(item)

# 每天有两条热搜汇总  11点  23点  会有重合数据  去重
filelist = list(filter(lambda x: str(x).find('23点') >= 0, file_list))
sum_list = []

i = 0
for file in filelist:
	# 遍历出每一个md文件   读取数据
	with file.open(encoding='utf-8') as f:
		lines = f.readlines()
	lines = [i.strip() for i in lines]   # 去除空字符
	data = list(filter(None, lines))     # 去除掉列表中的空子串
	data = data[1:101]
	con = data[::2]     # 热搜内容
	rank = data[1::2]   # 热度
	date = re.findall('年(.+)2', str(file)) * len(con)
	for m in range(len(con)):
		con[m] = con[m].split('、')[-1]   # 字符串操作
	for n in range(len(rank)):
		rank[n] = re.findall(r'\d+', rank[n])[0]
	con_dic = {'日期': date, '热搜内容': con, '热度': rank}
	df = pd.DataFrame(con_dic)
	if i == 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 += 50

print('共{}条数据写入csv'.format(i))

运行效果如下:

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

作者:叶庭云 CSDN:https://yetingyun.blog.csdn.net/ 本文仅用于交流学习,未经作者允许,禁止转载,更勿做其他用途,违者必究。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、pathlib模块下 Path 类的基本使用
  • 二、与os模块用法的对比
  • 三、实战案例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档