每天学习一点Python——文件操作全攻略
大家好!今天我们来继续学习Python中文件操作的核心技能。无论是读写文本文件,还是处理CSV数据,文件操作都是编程中必不可少的环节。让我们从基础开始,一步步掌握这些实用技巧。
一、准备工作:导入必要的模块
首先,我们需要导入Python的pathlib模块,它是现代Python中处理文件路径的首选方式:
from pathlib import Path
📝 代码解释:
- •
from pathlib import Path:从pathlib模块导入Path类 - •
pathlib是Python 3.4+引入的路径操作库,比传统的os.path更直观
二、查看当前工作目录
在开始文件操作前,我们先了解一下程序当前工作目录:
print(Path.cwd())
📝 代码解释:
- •
Path.cwd():获取"当前工作目录"(Current Working Directory)
三、创建目录和子目录
现在我们来创建自己的文件夹(昨天已经创建过了,今天再复习下):
# 在用户主目录下创建new_directory文件夹
new_dir = Path.home() / "new_directory"
new_dir.mkdir(exist_ok=True)
📝 代码解释:
- •
/操作符:用于拼接路径,比传统字符串拼接更安全
# 创建多级子目录:new_directory/folder_a/folder_b
nested_dir = new_dir / "folder_a" / "folder_b"
nested_dir.mkdir(parents=True)
📝 逐行解析:
| | |
|---|
| new_dir = Path.home() / "new_directory" | 创建路径对象,指向用户主目录下的new_directory |
| new_dir.mkdir(exist_ok=True) | |
| nested_dir = new_dir / "folder_a" / "folder_b" | |
| nested_dir.mkdir(parents=True) | |
四、文件的基本操作
1. 创建文件
file_path = new_dir / "file1.txt"
file_path.touch()
📝 代码解释:
- •
touch():创建一个空文件,类似Linux的touch命令
2. 移动/重命名文件
source = new_dir / "file1.txt"
destination = new_dir / "folder_a" / "file1.txt"
source.replace(destination)
✅ 执行结果:
WindowsPath('C:/Users/用户名/new_directory/folder_a/file1.txt')
📝 代码解释:
- •
replace():移动文件到新位置,如果目标位置已有文件则覆盖
⚠️ 重要提醒: 如果源文件不存在会报错!请确保文件已创建再进行移动操作。
3. 删除文件
file_to_delete = new_dir / "folder_a" / "file1.txt"
file_to_delete.unlink()
📝 代码解释:
五、批量操作实战
一次性创建多个测试文件和目录:
test_dir = new_dir
print(test_dir.exists()) # 检查目录是否存在
files_to_create = [
test_dir / "README.md", # 文本文件
test_dir / "config.json", # 配置文件
test_dir / "data.csv", # 数据文件
test_dir / "logs", # 日志目录
]
for item in files_to_create:
if item.suffix: # 如果有后缀就是文件
item.touch()
print(f"已创建文件:{item.name}")
else: # 没有后缀就是目录
item.mkdir(exist_ok=True)
print(f"已创建目录:{item.name}")
✅ 执行结果:
True
已创建文件:README.md
已创建文件:config.json
已创建文件:data.csv
已创建目录:logs
🔑 关键概念:
- •
suffix属性:获取文件后缀名(如.txt、.csv)
💡 实用技巧: 通过item.suffix可以判断是文件还是目录,因为目录通常没有后缀名。
六、删除目录的两种方式
方式1:删除空目录
empty_dir = new_dir / "logs"
empty_dir.rmdir() # 只能删除空目录
方式2:删除非空目录
import shutil
shutil.rmtree(new_dir / "folder_a") # 强力删除,包括所有内容
🆚 两种删除方式对比
| | | |
|---|
path.rmdir() | | | |
shutil.rmtree(path) | | | |
🔍 rmtree()工作原理详解
对于代码:
shutil.rmtree(new_dir/"folder_a")
路径解析:
- •
new_dir = C:\Users\用户名\new_directory - •
new_dir/"folder_a" = C:\Users\用户名\new_directory\folder_a
删除范围图示:
new_directory/ ← 不会被删除
├── folder_a/ ← 从这里开始删除(包括里面的所有内容)
│ ├── file1.txt ← 被删除
│ ├── file2.txt ← 被删除
│ └── folder_b/ ← 被删除
├── README.md ← 不会被删除
└── config.json ← 不会被删除
📌 核心理解:rmtree()只删除你指定的路径及其子内容,不会向上删除父目录。
七、文本文件的读写操作
1. 写入文本文件
path = new_dir / "hello.txt"
with path.open(mode='w', encoding="utf-8") as file:
file.write("Hello,World!")
chars_written = file.write("\nWelcome to Python!")
print(f"写入的字符数:{chars_written}")
✅ 执行结果:
写入的字符数:19
逐行解释代码
第一行:创建路径对象
path = new_dir / "hello.txt"
说明:此时只是定义路径,文件还不存在。
第二行:打开文件
with path.open(mode='w', encoding="utf-8") as file:
说明:mode='w'表示写入模式,文件在此时被创建。
第三行:第一次写入
file.write("Hello,World!")
说明:写入12个字符到新文件。
第四行:第二次写入
chars_written = file.write("\nWelcome to Python!")
说明:
关键问题解答
问: 是不是用两次with ... as file:就会覆盖了?
答: 完全正确!这就是关键区别。
两种情况对比
情况1:单次with,多次write(不会覆盖)
# 在同一个with块内连续写入
with path.open(mode='w', encoding="utf-8") as file:
file.write("Hello,World!") # 第一次写入
file.write("\nWelcome to Python!") # 第二次接着写
结果:两行内容都保留。
情况2:两次with,每次都覆盖
# 第一次with块
with path.open(mode='w', encoding="utf-8") as file:
file.write("Hello,World!") # 写入第一行
# with结束,文件关闭
# 第二次with块(重新打开)
with path.open(mode='w', encoding="utf-8") as file:
file.write("New Content") # 完全覆盖之前的全部内容
结果:只看到"New Content",之前的"Hello,World!"被覆盖。
核心原理图示
情况1(单次with):
打开文件 → 写"Hello,World!" → 继续写"Welcome" → 关闭文件
↓
文件内容:两行都保留
情况2(两次with):
第一次:打开文件 → 写"Hello" → 关闭文件
第二次:重新打开文件 → 写"New" → 关闭文件
↓
文件内容:只有"New"(之前的内容被清空)
简单记忆规则
- 1. 同一个
with块内:多次write()是追加
2. 读取整个文件
with path.open(mode="r", encoding="utf-8") as file:
content = file.read()
print("文件内容:")
print(content)
代码解释:
✅ 执行结果:
文件内容:
Hello,World!
Welcome to Python!
3. 逐行读取文件
with path.open(mode='r', encoding="utf-8") as file:
for line in file.readlines():
print(f"行内容:{line.strip()}")
代码解释:
- •
readlines():读取所有行,返回字符串列表
✅ 执行结果:
行内容:Hello,World!
行内容:Welcome to Python!
🧹 strip()方法详解
strip()主要用于去除字符串首尾的空白字符,但功能更强大:
基本用法:
text = " Hello World! "
print(text.strip()) # "Hello World!" ← 去掉首尾空格
去除指定字符:
text = "***Hello***"
print(text.strip("*")) # "Hello" ← 去掉首尾星号
去除多种字符:
text = " \n \t Hello! \t\n "
print(text.strip(" \n\t!")) # "Hello" ← 去掉空格、换行、制表符、感叹号
strip()家族对比:
| | | |
|---|
strip() | | " abc ".strip() | "abc" |
lstrip() | | " abc ".lstrip() | "abc " |
rstrip() | | " abc ".rstrip() | " abc" |
💡 重要特性:strip()只处理字符串首尾,中间内容保持不变!
4. 追加内容到文件
with path.open(mode="a", encoding="utf-8") as file:
file.write("\nThis is appended text.")
📝 代码解释:
- •
mode="a":追加模式,在文件末尾添加内容
📊 文件打开模式总结:
⚠️ 重要提示: 文件打开模式要记牢,特别是'w'和'a'的区别!
八、CSV文件的读写操作
1. CSV文件写入(列表格式)
先准备一些数据:
import csv
from pathlib import Path
daily_temperatures = [
[68, 65, 68, 70, 74, 72],
[67, 67, 70, 72, 72, 70],
[68, 70, 74, 76, 74, 73],
]
写入CSV文件:
file_path = new_dir / "temperatures.csv"
with file_path.open(mode="w", encoding="utf-8", newline="") as file:
writer = csv.writer(file)
writer.writerows(daily_temperatures)
代码解释:
open()中的newline=""参数:
CSV写入器:
- •
with ... as file:使用上下文管理器,自动管理文件资源,确保文件正确关闭 - •
writer.writerows():批量写入多行数据
2. CSV文件读取
with file_path.open(mode="r", encoding="utf-8", newline="") as file:
reader = csv.reader(file)
for row in reader:
print(f"原始行:{row}")
int_row = [int(value) for value in row]
print(f"转换后:{int_row}")
代码解释:
- • CSV读取的所有值都是字符串类型,需要时需手动转换
- •
[int(value) for value in row]:列表推导式,将每行的字符串转为整数
✅ 执行结果:
原始行:['68', '65', '68', '70', '74', '72']
转换后:[68, 65, 68, 70, 74, 72]
原始行:['67', '67', '70', '72', '72', '70']
转换后:[67, 67, 70, 72, 72, 70]
原始行:['68', '70', '74', '76', '74', '73']
转换后:[68, 70, 74, 76, 74, 73]
3. 带表头的CSV写入(字典格式)
people = [
{"name": "Veronica", "age": 29},
{"name": "Audrey", "age": 32},
{"name": "Sam", "age": 24},
]
file_path = new_dir / "people.csv"
with file_path.open(mode="w", encoding="utf-8", newline="") as file:
writer = csv.DictWriter(file, fieldnames=["name", "age"])
writer.writeheader()
writer.writerows(people)
代码解释:
csv.DictWriter的优点:
关键参数:
- •
fieldnames=["name", "age"]:定义CSV的列名和顺序 - •
writerows(people):写入数据行
✅ 生成的文件内容:
name,age
Veronica,29
Audrey,32
Sam,24
4. 带表头的CSV读取
with file_path.open(mode="r", encoding="utf-8", newline="") as file:
reader = csv.DictReader(file)
for row in reader:
print(f"姓名:{row['name']}, 年龄:{row['age']}")
代码解释:
- •
csv.DictReader():创建字典读取器 - • 通过
row['name']访问特定列,代码更清晰
✅ 执行结果:
姓名:Veronica, 年龄:29
姓名:Audrey, 年龄:32
姓名:Sam, 年龄:24
九、两种CSV写法的对比
| csv.writer | csv.DictWriter |
|---|
| 数据结构 | | |
| 列顺序 | | |
| 表头处理 | | |
| 代码可读性 | | |
| 适用场景 | | |
| 数据来源 | | |
💡 建议: 从数据库或API获取的数据通常是字典格式,使用DictWriter更方便!
十、总结
📚 今日重点回顾
- 1. 路径操作:使用
pathlib模块创建、移动、删除文件和目录 - 2. 文本文件:掌握
read()、write()、readlines()等基本操作 - 3. CSV文件:学会用
csv.writer和csv.DictWriter处理表格数据
🔑 关键要点
- • 文件操作完一定要关闭(或用
with语句自动管理) - • CSV读写注意
newline=""参数确保跨平台兼容 - • 大文件不要用
read()一次性读取,改用逐行处理
📦 资源获取提示
关注「码农自习室」,后台回复关键词 Python学习,即可获取本文完整代码,一起动手掌握高效编程的核心技巧!
❤️ 支持我们
如果觉得本文对你有帮助,欢迎点赞 + 关注,您的支持是我们持续创作优质内容的最大动力!
📚 学习资源说明
本文内容是基于《Python Basics: A Practical Introduction to Python 3》(Real Python)一书的学习笔记整理。
这本书是一本非常优秀的Python入门教材,推荐给所有想要系统学习Python的朋友们。
这本书的特点:
跟着这本书学习,配合我的笔记整理,相信你能更快掌握Python编程!
让我们一起坚持学习,每天进步一点点!💪