本文整理 Python3 核心文件 / 文件夹操作,聚焦
os
os.path
shutil传统模块与 Python3.4+ 新增的
pathlib现代化模块,涵盖路径处理、文件读写、目录管理、批量操作等!
一、核心模块与环境说明
1. 模块特性对比
| 模块 | 核心优势 | 适用场景 | 版本要求 |
|---|---|---|---|
os |
系统底层操作,功能全面 | 进程管理、系统命令调用、基础文件操作 | Python3 全版本 |
os.path |
路径字符串处理,跨平台兼容 | 路径拼接、判断、拆分 | Python3 全版本 |
shutil |
高级文件操作,封装常用功能 | 复制、移动、删除、压缩 | Python3 全版本 |
pathlib |
面向对象 API,语法简洁,可读性强 | 路径处理、文件读写、目录遍历 | Python3.4+ |
2. 基础导入语句
python
# 传统模块(兼容所有Python3版本)
import os
import os.path as op
import shutil
# 现代化模块(Python3.4+推荐)
from pathlib import Path
二、路径处理操作
1. 传统方式(os + os.path)
python
# 1. 获取当前工作目录
current_dir = os.getcwd()
print("当前目录:", current_dir)
# 2. 路径拼接(避免硬编码分隔符)
file_path = op.join(current_dir, "data", "test.txt")
print("拼接路径:", file_path) # 输出: 跨平台自动适配(Windows\,Linux/Mac/)
# 3. 路径拆解
print("目录名:", op.dirname(file_path)) # 输出: 父目录路径
print("文件名:", op.basename(file_path)) # 输出: test.txt
print("扩展名:", op.splitext(file_path)[1]) # 输出: .txt
print("文件名(无扩展名):", op.splitext(op.basename(file_path))[0]) # 输出: test
# 4. 路径判断
print("是否为文件:", op.isfile(file_path))
print("是否为目录:", op.isdir(current_dir))
print("路径是否存在:", op.exists(file_path))
print("是否为绝对路径:", op.isabs(file_path))
# 5. 获取绝对路径
abs_path = op.abspath("relative/path")
print("绝对路径:", abs_path)
2. 现代化方式(pathlib)
python
# 1. 创建路径对象(推荐方式)
current_dir = Path.cwd() # 获取当前目录
file_path = current_dir / "data" / "test.txt" # 路径拼接(支持/运算符)
print("路径对象:", file_path) # 自动适配系统路径格式
# 2. 路径属性(直观易用)
print("目录名:", file_path.parent) # 父目录(可链式调用:file_path.parent.parent)
print("文件名:", file_path.name) # 完整文件名:test.txt
print("扩展名:", file_path.suffix) # 扩展名:.txt
print("文件名(无扩展名):", file_path.stem) # 文件名:test
print("绝对路径:", file_path.absolute())
# 3. 路径判断(面向对象方法)
print("是否为文件:", file_path.is_file())
print("是否为目录:", current_dir.is_dir())
print("路径是否存在:", file_path.exists())
print("是否为绝对路径:", file_path.is_absolute())
# 4. 路径匹配(支持glob模式)
print("匹配所有txt文件:", list(current_dir.glob("*.txt")))
print("递归匹配所有py文件:", list(current_dir.rglob("*.py"))) # Python3.5+
三、文件操作
1. 基础读写
python
# 1. 文本文件写入(覆盖模式)
with open("test.txt", "w", encoding="utf-8") as f:
f.write("Python文件操作备忘录\n")
f.writelines(["第一行内容\n", "第二行内容\n"])
# 2. 文本文件读取
with open("test.txt", "r", encoding="utf-8") as f:
content = f.read() # 读取全部内容
# content = f.readline() # 读取一行
# content = f.readlines() # 读取所有行到列表
# 3. 二进制文件操作(如图片、视频)
with open("image.jpg", "rb") as f:
binary_data = f.read() # 读取二进制数据
with open("image_copy.jpg", "wb") as f:
f.write(binary_data) # 写入二进制数据
# 4. 文件指针控制(seek/tell)
with open("test.txt", "r", encoding="utf-8") as f:
f.seek(10) # 移动到第10字节位置
print("当前指针位置:", f.tell()) # 返回当前指针位置
f.seek(0, 2) # 移动到文件末尾(适合追加前定位)
2. pathlib 简化读写
python
# 1. 文本文件读写(一行搞定)
file = Path("test_pathlib.txt")
file.write_text("pathlib 简化文件操作\n", encoding="utf-8") # 写入
content = file.read_text(encoding="utf-8") # 读取
print("读取内容:", content)
# 2. 二进制文件读写
bin_file = Path("image_copy.jpg")
bin_file.write_bytes(binary_data) # 写入二进制
read_bin = bin_file.read_bytes() # 读取二进制
# 3. 逐行读取(大文件友好)
with file.open("r", encoding="utf-8") as f:
for line in f:
print(line.strip())
四、目录操作(创建 / 删除 / 遍历)
1. 传统方式(os + shutil)
python
# 1. 创建目录
os.mkdir("new_dir") # 创建单级目录(父目录不存在则报错)
os.makedirs("parent_dir/child_dir", exist_ok=True) # 创建多级目录(Python3.2+支持exist_ok)
# 2. 删除目录
os.rmdir("new_dir") # 删除空目录(非空则报错)
shutil.rmtree("parent_dir") # 强制删除目录(含所有子文件/目录,慎用!)
# 3. 目录遍历
# 方式1:列出目录内容(仅当前级)
for name in os.listdir("target_dir"):
full_path = op.join("target_dir", name)
if op.isfile(full_path):
print("文件:", name)
else:
print("目录:", name)
# 方式2:递归遍历目录(Python3.5+)
for root, dirs, files in os.walk("target_dir"):
print(f"当前目录: {root}")
print(f"子目录: {dirs}")
print(f"文件: {files}")
2. 现代化方式(pathlib)
python
# 1. 创建目录
Path("new_dir").mkdir(exist_ok=True) # 单级目录
Path("parent_dir/child_dir").mkdir(parents=True, exist_ok=True) # 多级目录(parents=True必选)
# 2. 删除目录
Path("new_dir").rmdir() # 删除空目录
shutil.rmtree("parent_dir") # pathlib无强制删除,需配合shutil
# 3. 目录遍历
# 方式1:遍历当前级内容
for item in Path("target_dir").iterdir():
if item.is_file():
print("文件:", item.name)
else:
print("目录:", item.name)
# 方式2:递归遍历(推荐,简洁直观)
for file in Path("target_dir").rglob("*"):
if file.is_file():
print("递归找到文件:", file)
五、高级操作
1. 文件复制与移动
python
# 1. 文件复制
shutil.copy("source.txt", "dest.txt") # 复制文件内容和权限
shutil.copy2("source.txt", "dest_dir/") # 复制文件+保留元数据(创建时间等)
shutil.copyfile("source.bin", "dest.bin") # 仅复制文件内容(无权限)
# 2. 目录复制(递归复制所有内容)
shutil.copytree("source_dir", "dest_dir", dirs_exist_ok=True) # Python3.8+支持覆盖
# 3. 文件/目录移动(剪切)
shutil.move("source.txt", "target_dir/") # 移动文件到目录
shutil.move("old_dir", "new_dir") # 重命名目录(目标不存在)或移动到目标目录
# 4. 压缩与解压
# 压缩(创建zip包)
shutil.make_archive("archive", "zip", root_dir="target_dir") # 生成archive.zip
# 解压(提取zip包)
shutil.unpack_archive("archive.zip", extract_dir="unpack_dir")
2. 批量文件处理
python
# 场景1:按扩展名分类文件(如图片、文档)
from pathlib import Path
import shutil
ext_map = {
".jpg": "Images",
".png": "Images",
".pdf": "Documents",
".docx": "Documents",
".mp3": "Music"
}
source_dir = Path(".")
for file in source_dir.iterdir():
if file.is_file() and file.suffix in ext_map:
target_dir = Path(ext_map[file.suffix])
target_dir.mkdir(exist_ok=True) # 确保目标目录存在
shutil.move(file, target_dir / file.name) # 移动文件
print(f"已移动: {file.name} -> {target_dir.name}")
# 场景2:批量重命名文件(如添加前缀)
for i, file in enumerate(Path("images").glob("*.jpg")):
new_name = f"photo_{i:03d}{file.suffix}" # 生成001、002格式
file.rename(file.parent / new_name)
print(f"重命名: {file.name} -> {new_name}")
# 场景3:统计目录下文件大小
def get_dir_size(dir_path):
total_size = 0
for file in Path(dir_path).rglob("*"):
if file.is_file():
total_size += file.stat().st_size # 获取文件大小(字节)
return total_size / (1024 * 1024) # 转换为MB
print(f"目录总大小: {get_dir_size('target_dir'):.2f} MB")
六、避坑指南
-
路径分隔符问题 :永远使用
os.path.join()或pathlib的/运算符拼接路径,避免直接写死\或/,确保跨平台兼容; -
文件编码规范 :读写文本文件时必须指定
encoding="utf-8",避免默认编码导致中文乱码(Windows 默认 gbk,Linux/Mac 默认 utf-8); -
资源释放原则 :始终使用
with语句操作文件,自动关闭文件句柄,避免资源泄露; -
删除操作警告 :
shutil.rmtree()和os.remove()是不可逆操作,生产环境需添加二次确认逻辑,或先备份再删除; -
版本兼容性:
-
pathlib.rglob()需 Python3.5+; -
shutil.copytree(dirs_exist_ok=True)需 Python3.8+; -
若需兼容低版本,优先使用
os+os.path组合;
-
大文件处理 :避免使用
read()一次性读取超大文件(占用内存),推荐readline()逐行读取或pathlib迭代器; -
权限问题 :复制 / 移动文件时注意权限继承,
shutil.copy2()保留元数据更适合需要追溯文件历史的场景。