网站首页 > 技术文章 正文
with 语句(上下文管理器)是 Python 中处理文件操作的首选方式,它能自动管理资源,确保文件正确关闭,即使发生异常也是如此。下面详细介绍如何正确使用 with 进行文件读写操作。
一、为什么使用with语句?
传统文件操作方式的问题:
file = open('example.txt', 'r')
content = file.read()
file.close() # 必须显式关闭,否则可能导致资源泄漏
潜在问题:
- 忘记调用 close() 方法
- 代码抛出异常导致 close() 未被执行
- 代码复杂时难以确保所有路径都关闭文件
with 语句的优势:
- 自动资源管理:离开 with 块时自动关闭文件
- 异常安全:即使发生异常也会确保文件关闭
- 代码简洁:减少样板代码
- 可读性强:明确显示文件的作用域
二、基本语法
1. 读取文件
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
# 在此块内操作文件
# 离开with块后文件自动关闭
print(content) # 文件已关闭,但内容已读取到变量
2. 写入文件
with open('output.txt', 'w', encoding='utf-8') as file:
file.write('Hello, World!\n')
file.write('这是第二行\n')
# 文件自动关闭,内容已写入磁盘
3. 追加内容
with open('output.txt', 'a', encoding='utf-8') as file:
file.write('这是追加的内容\n')
三、高级用法
1. 同时处理多个文件
with open('source.txt', 'r') as src, open('destination.txt', 'w') as dst:
content = src.read()
dst.write(content)
# 两个文件都会自动关闭
2. 二进制文件操作
# 复制图片文件
with open('input.jpg', 'rb') as src, open('output.jpg', 'wb') as dst:
dst.write(src.read())
3. 异常处理
try:
with open('config.json', 'r') as file:
data = json.load(file)
except FileNotFoundError:
print("配置文件不存在")
except json.JSONDecodeError:
print("配置文件格式错误")
except IOError as e:
print(f"文件操作错误: {e}")
4. 使用with和iter高效读取大文件
with open('large_file.log', 'r') as file:
for line in iter(file.readline, ''): # 逐行读取,内存高效
process_line(line)
四、with语句的工作原理
with 语句实际上使用了上下文管理器协议,open() 返回的文件对象实现了这个协议:
- 进入 with 块时调用 __enter__() 方法
- 离开 with 块时调用 __exit__() 方法(即使发生异常)
- __exit__() 方法中包含了文件关闭的逻辑
等效的传统写法:
file = open('example.txt', 'r')
try:
content = file.read()
finally:
file.close()
五、实际应用示例
示例1:配置文件读取
import json
def load_config(config_path):
try:
with open(config_path, 'r', encoding='utf-8') as file:
return json.load(file)
except (FileNotFoundError, json.JSONDecodeError) as e:
print(f"加载配置失败: {e}")
return {}
示例2:日志文件处理
def process_log_file(log_path):
with open(log_path, 'r') as log_file:
for line in log_file:
if 'ERROR' in line:
send_alert(line.strip())
def send_alert(error_msg):
with open('alerts.log', 'a') as alert_file:
alert_file.write(f"{datetime.now()}: {error_msg}\n")
示例3:CSV 数据处理
import csv
def process_csv(input_path, output_path):
with open(input_path, 'r') as infile, open(output_path, 'w', newline='') as outfile:
reader = csv.DictReader(infile)
writer = csv.DictWriter(outfile, fieldnames=reader.fieldnames)
writer.writeheader()
for row in reader:
if is_valid(row):
writer.writerow(process_row(row))
六、注意事项
- 编码问题:始终明确指定文件编码(推荐 utf-8)
with open('file.txt', 'r', encoding='utf-8') as f:
- 缓冲设置:处理大量数据时可调整缓冲区大小
with open('large.data', 'wb', buffering=1024*1024) as f: # 1MB缓冲
- 文件位置:with 块结束后文件指针位置
with open('file.txt', 'r+') as f:
content = f.read() # 读取后指针在文件末尾
f.seek(0) # 需要重置指针才能再次读取或写入
- 不要重复使用文件对象:
with open('file.txt') as f:
data1 = f.read() # 第一次读取
data2 = f.read() # 第二次读取将得到空字符串!
七、性能考虑
对于性能关键代码:
- 大文件使用逐行或分块读取
with open('huge.log') as f:
for line in f: # 内存高效
process(line)
- 多次小写入可考虑先收集数据再一次性写入
data = []
for item in items:
data.append(format_item(item))
with open('output.txt', 'w') as f:
f.writelines(data)
总结
- 总是使用 with 语句处理文件操作
- 明确指定文件编码(特别是文本文件)
- 根据需要选择合适的读写模式
- 大文件使用迭代方式处理
- 注意文件指针位置
- 考虑使用 pathlib 进行路径操作(Python 3.4+)
猜你喜欢
- 2025-05-02 Python Flask服务器模块,模板继承和模板变量
- 2025-05-02 「python+django」django中使用jinja2模板
- 2025-05-02 docopt,一个超厉害的 Python 库!
- 2025-05-02 一文带您了解Jinja2:探索Python模板引擎
- 2025-05-02 python优雅执行SQL模板语句(pycharm执行sql)
- 2025-05-02 程序员简历例句—范例Java、Python、C++模板
- 2025-05-02 快速生成定制化的Word文档:Python实践指南
- 2025-05-02 jinjia2:一个超级模版Python库(python jinja2模板)
- 2025-05-02 Python | Django 模板 template(python模板方法)
- 05-27程序员用 Python 爬取抖音高颜值美女
- 05-27YOLO v3、FaceNet和SVM的人脸检测识别系统源码(python)分享
- 05-27「工具推荐」世界上最简单的人脸识别库 44.7 star
- 05-27开源人脸识别系统源码推荐
- 05-27Go 人脸识别教程
- 05-27Python 深度学习之人脸识别(yolo+facenet)
- 05-27简单的Py人脸识别
- 05-27Python编程 - 基于OpenCV实现人脸识别(实践篇)爬虫+人脸识别
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 82℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)