pathlib 模块提供了表示文件系统路径的类,可适用于不同的操作系统。
本文介绍如何使用 pathlib 模块中的 Path 类读写文件、操纵文件路径和基础文件系统,统计目录下的文件类型以及查找匹配目录下某一类型文件等。
from pathlib import Path
currentPath = Path.cwd()
homePath = Path.home()
print("文件当前所在目录:%s\n用户主目录:%s" %(currentPath, homePath))
输出:
文件当前所在目录:/home/abc/test
用户主目录:/home/abc
from pathlib import Path
currentPath = Path.cwd()
newPath = currentPath / 'python-100'
print("新目录为:%s" %(newPath))
输出:
新目录为:/home/abc/test/python-100
from pathlib import Path
currentPath = Path.cwd()
makePath = currentPath / 'python-100'
makePath.mkdir() # 在当前目录下创建一个子目录 python-100
print("创建的目录为:%s" %(makePath))
from pathlib import Path
currentPath = Path.cwd()
delPath = currentPath / 'python-100'
delPath.rmdir() # 在当前目录下删除子目录python-100
print("删除的目录为:%s" %(delPath))
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())
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)
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。
# 使用 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))
本文介绍了 Python 的 pathlib 模块,让大家了解如何使用 pathlib 模块读写文件、操纵文件路径和基础文件系统,统计目录下的文件类型以及查找匹配目录下某一类型文件等。
【1】https://blog.csdn.net/qdPython/article/details/125209515