pathlib 模块提供了表示文件系统路径的类,可适用于不同的操作系统。使用 pathlib 模块,相比于 os 模块可以写出更简洁,易读的代码。pathlib 模块中的 Path 类继承自 PurePath,对 PurePath 中的部分方法进行了重载,相比于 os.path 有更高的抽象级别。本文将带你学习如何使用 pathlib 模块中的 Path 类读写文件、操纵文件路径和基础文件系统,统计目录下的文件类型以及查找匹配目录下某一类型文件等。下面就开始进入我们的学习时刻。 1 获取目录
应用示例: from pathlib import Path currentPath = Path.cwd() homePath = Path.home() print("文件当前所在目录:%s\n用户主目录:%s" %(currentPath, homePath)) 2 目录拼接斜杠 / 操作符用于拼接路径,比如创建子路径。 应用示例: from pathlib import Path currentPath = Path.cwd() newPath = currentPath / 'python-100' print("新目录为:%s" %(newPath)) 3 创建、删除目录
应用示例: from pathlib import Path currentPath = Path.cwd() makePath = currentPath / 'python-100' makePath.mkdir() print("创建的目录为:%s" %(nmakePath)) from pathlib import Path currentPath = Path.cwd() delPath = currentPath / 'python-100' delPath.rmdir() print("删除的目录为:%s" %(delPath)) 4 读写文件
应用示例: from pathlib import Path currentPath = Path.cwd() mkPath = currentPath / 'python-100.txt' with mkPath.open('w') as f: # 创建并以 "w" 格式打开 python-100.txt 文件。 f.write('python-100') # 写入 python-100 字符串。 f = open(mkPath, 'r') print("读取的文件内容为:%s" % f.read()) f.close() from pathlib import Path currentPath = Path.cwd() mkPathText = currentPath / 'python-100-text.txt' mkPathText.write_text('python-100') print("读取的文件内容为:%s" % mkPathText.read_text())
str2byte = bytes('python-100', encoding = 'utf-8') mkPathByte = currentPath / 'python-100-byte.txt' mkPathByte.write_bytes(str2byte) print("读取的文件内容为:%s" % mkPathByte.read_bytes()) 5 获取文件所在目录的不同部分字段
from pathlib import Path txtPath = Path('python-100.txt') nowPath = txtPath.resolve() print("文件的完整路径为:%s" % nowPath) print("文件完整名称为(文件名+后缀名):%s" % nowPath.name) print("文件名为:%s" % nowPath.stem) print("文件后缀名为:%s" % nowPath.suffix) print("文件所在的文件夹名为:%s" % nowPath.parent) print("文件所在的盘符为:%s" % nowPath.anchor) 6 文件、路径是否存在判断
from pathlib import Path currentPath = Path.cwd() / 'python'
print(currentPath.exists()) # 判断是否存在 python 文件夹,此时返回 False。 print(currentPath.is_dir()) # 判断是否存在 python 文件夹,此时返回 False。
currentPath.mkdir() # 创建 python 文件夹。
print(currentPath.exists()) # 判断是否存在 python 文件夹,此时返回 True。 print(currentPath.is_dir()) # 判断是否存在 python 文件夹,此时返回 True。
currentPath = Path.cwd() / 'python-100.txt'
print(currentPath.exists()) # 判断是否存在 python-100.txt 文件,此时文件未创建返回 False。 print(currentPath.is_file()) # 判断是否存在 python-100.txt 文件,此时文件未创建返回 False。
f = open(currentPath,'w') # 创建 python-100.txt 文件。 f.close()
print(currentPath.exists()) # 判断是否存在 python-100.txt 文件,此时返回 True。 print(currentPath.is_file()) # 判断是否存在 python-100.txt 文件,此时返回 True。 7 文件统计以及匹配查找
# 使用 Path.iterdir() 获取当前文件下的所有文件,并根据后缀名统计其个数。 import pathlib from collections import Counter currentPath = pathlib.Path.cwd() gen = (i.suffix for i in currentPath.iterdir()) print(Counter(gen)) import pathlib from collections import Counter currentPath = pathlib.Path.cwd() gen = (i.suffix for i in currentPath.glob('*.txt')) # 获取当前文件下的所有 txt 文件,并统计其个数。 print(Counter(gen)) gen = (i.suffix for i in currentPath.rglob('*.txt')) # 获取目录中子文件夹下的所有 txt 文件,并统计其个数。 print(Counter(gen)) 8 总结本文给大家介绍了 Python 的 pathlib 模块,为 Python 工程师对该模块的使用提供了支撑,让大家了解如何使用 pathlib 模块读写文件、操纵文件路径和基础文件系统,统计目录下的文件类型以及查找匹配目录下某一类型文件等。 参考资料https:///python-pathlib/ https://docs./zh-cn/3/library/pathlib.html
系列文章 第24天:Python Standard Library 02 |
|