Python3 File 文件方法:跟文件打交道,这一篇就够了
我是陈默,一个正拼命上岸的码农。
程序的数据存在哪?
你关掉终端,变量就没了。下次运行,从头开始。
但现实不是这样的。你的配置要保存,你的记录要留下,你的数据要持久化。
这就需要文件操作——把数据写进去,下次还能读出来。
今天我们把 Python 文件操作从头到尾讲清楚。
1. 打开文件:open()
所有文件操作的第一步:打开文件。
f = open("test.txt", "r")
第二个参数是模式,决定了你能对文件做什么:
加 "b" 表示二进制模式:"rb"、"wb"、"ab"。
最常用的是 "r"(读)、"w"(写)、"a"(追加)三个。
2. 永远用 with 打开文件
传统写法(不推荐)
f = open("test.txt", "r")content = f.read()f.close() # 别忘了关!
with 写法(推荐)
with open("test.txt", "r") as f: content = f.read()# 离开 with 块,文件自动关闭
为什么推荐 with?
从今天起,别再手动 close() 了。
3. 读取文件
read():一口气读完
with open("test.txt", "r", encoding="utf-8") as f: content = f.read() print(content)
read() 把整个文件读成一个字符串。
小文件没问题。大文件(几百 MB)会把内存吃满。
readline():每次读一行
with open("test.txt", "r", encoding="utf-8") as f: line1 = f.readline() line2 = f.readline() print(line1) print(line2)
readlines():读成列表
with open("test.txt", "r", encoding="utf-8") as f: lines = f.readlines() print(lines)# 输出: ['第一行\n', '第二行\n', '第三行\n']
每行末尾带着 \n。用 strip() 去掉:
lines = [line.strip() for line in lines]
直接遍历(最推荐)
with open("test.txt", "r", encoding="utf-8") as f:for line in f: print(line.strip())
这种方式内存友好。不管文件多大,每次只有一行在内存里。
4. 写入文件
write():写入字符串
with open("output.txt", "w", encoding="utf-8") as f: f.write("第一行\n") f.write("第二行\n") f.write("第三行\n")
注意 "w" 模式会覆盖原文件。原来的内容全没了。
writelines():写入列表
lines = ["苹果\n", "香蕉\n", "橙子\n"]with open("fruits.txt", "w", encoding="utf-8") as f: f.writelines(lines)
writelines() 不会自动加换行符。你需要自己在字符串里加 \n。
追加内容
with open("output.txt", "a", encoding="utf-8") as f: f.write("这是追加的内容\n")
"a" 模式在文件末尾追加,不会覆盖原有内容。
5. 文件指针:控制读写的位置
文件有个"光标",记录当前读到哪了。
tell():查看当前位置
with open("test.txt", "r") as f: print(f.tell()) # 输出: 0(开头) content = f.read(5) print(f.tell()) # 输出: 5(读了5个字节后)
seek():跳到指定位置
with open("test.txt", "r") as f: content = f.read() # 读全部 f.seek(0) # 跳回开头 content2 = f.read() # 再读一遍
seek(0) 就是回到文件开头。适合需要读两遍的场景。
6. 处理 CSV 文件
CSV 是最常见的数据格式。Python 自带 csv 模块。
读取 CSV
import csvwith open("scores.csv", "r", encoding="utf-8") as f: reader = csv.reader(f)for row in reader: print(row)# 输出(每行是一个列表):# ['姓名', '分数']# ['陈默', '85']# ['小明', '92']
读取为字典
import csvwith open("scores.csv", "r", encoding="utf-8") as f: reader = csv.DictReader(f)for row in reader: print(f"{row['姓名']}:{row['分数']}分")# 输出:# 陈默:85分# 小明:92分
DictReader 把每行变成字典,用表头当键。比用索引方便多了。
写入 CSV
import csvstudents = [ {"name": "陈默", "score": 85}, {"name": "小明", "score": 92}, {"name": "小红", "score": 78},]# 写入with open("output.csv", "w", newline="", encoding="utf-8") as f: writer = csv.DictWriter(f, fieldnames=["name", "score"]) writer.writeheader() # 写表头 writer.writerows(students) # 写数据
7. 处理 JSON 文件
JSON 是互联网上最流行的数据格式。
写入 JSON
import jsondata = {"name": "陈默","age": 25,"skills": ["Python", "SQL", "JavaScript"]}with open("data.json", "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2)
ensure_ascii=False:让中文正常显示
读取 JSON
import jsonwith open("data.json", "r", encoding="utf-8") as f: data = json.load(f)print(data["name"]) # 输出: 陈默print(data["skills"][0]) # 输出: Python
JSON 文件读出来就是 Python 的字典或列表,直接用就行。
8. 处理二进制文件
图片、音频、压缩包——这些是二进制文件,用 "b" 模式。
复制图片
with open("photo.jpg", "rb") as src: content = src.read()with open("photo_copy.jpg", "wb") as dst: dst.write(content)print("复制完成")
大文件分块读取
defcopy_large_file(src_path, dst_path, chunk_size=8192):"""分块复制大文件,不占内存"""with open(src_path, "rb") as src:with open(dst_path, "wb") as dst:whileTrue: chunk = src.read(chunk_size)ifnot chunk:break dst.write(chunk)copy_large_file("big_video.mp4", "backup.mp4")
每次只读 8KB,写 8KB。不管文件多大,内存占用都是固定的。
9. 实战:日志分析工具
from collections import Counterdefanalyze_log(filepath):"""分析日志文件,统计每个 IP 的访问次数""" ip_counter = Counter() error_count = 0 total_lines = 0with open(filepath, "r", encoding="utf-8") as f:for line in f: total_lines += 1# 提取 IP(假设 IP 在行首) parts = line.split()if parts: ip = parts[0] ip_counter[ip] += 1# 统计错误if"ERROR"in line: error_count += 1# 输出报告 print(f"总请求:{total_lines}") print(f"错误数:{error_count}") print(f"\n访问最多的 IP(Top 5):")for ip, count in ip_counter.most_common(5): print(f" {ip}: {count}次")# 假设有个日志文件# analyze_log("app.log")
10. 实战:批量处理文件内容
defprocess_files(input_dir, output_dir):"""读取目录下所有 txt 文件,处理后保存"""import os os.makedirs(output_dir, exist_ok=True)for filename in os.listdir(input_dir):ifnot filename.endswith(".txt"):continue input_path = os.path.join(input_dir, filename) output_path = os.path.join(output_dir, filename)with open(input_path, "r", encoding="utf-8") as f: lines = f.readlines()# 处理:去空行,去首尾空格 cleaned = [line.strip() + "\n"for line in lines if line.strip()]with open(output_path, "w", encoding="utf-8") as f: f.writelines(cleaned) print(f"处理完成:{filename}({len(lines)}行 → {len(cleaned)}行)")# 使用# process_files("raw_data", "cleaned_data")
最后
文件操作是每个程序员的必修课。你的数据不会永远活在内存里——总有一天要存到文件里。
记住三件事:
- 永远用
with open() 操作文件,别手动 close() - 读大文件用逐行遍历
for line in f,别用 read() - 加上
encoding="utf-8",避免中文乱码
我的建议:
写一个脚本,把你电脑上某个文件夹里的所有 .txt 文件读取一遍,统计每个文件有多少行、多少个字。这就是文件操作的实战练习。
数据处理的第一步,永远是读文件。这一步走稳了,后面的路才好走。
今天就到这里。
我是陈默,我们下期再见。
如果你觉得这篇文章有帮助,欢迎关注我。我会持续分享 Python 学习的干货。