Python文件读写推荐使用with语句,因它能自动关闭文件、确保异常安全且代码更简洁;结合open()函数指定文件路径、模式和encoding参数可高效处理不同编码的文本,避免乱码与资源泄漏。

Python中处理文件读写,核心在于使用内置的
open()
read()
write()
with
在Python里,文件读写操作其实挺直观的,但有些细节,比如文件模式和编码,需要特别注意。
1. 打开文件:open()
这是所有文件操作的起点。
open()
立即学习“Python免费学习笔记(深入)”;
# 示例:以只读模式打开一个文件
file_object = open('my_document.txt', 'r', encoding='utf-8')
# 稍后会讲到with语句,这只是一个基本演示常见的模式有:
'r'
'w'
'a'
'x'
FileExistsError
'b'
'r'
'w'
'a'
'rb'
'wb'
't'
'r'
'w'
'a'
'rt'
'wt'
'+'
'r'
'w'
'a'
'r+'
'w+'
2. 读取文件
文件对象提供了多种读取方法:
read(size=-1)
size
readline(size=-1)
readlines()
# 假设我们有一个名为 'example.txt' 的文件,内容如下:
# Hello, Python!
# This is a test file.
# Line three.
# 使用with语句读取文件内容,这是我个人最推荐的方式
with open('example.txt', 'r', encoding='utf-8') as f:
content = f.read() # 读取所有内容
print("全部内容:\n", content)
print("-" * 20)
with open('example.txt', 'r', encoding='utf-8') as f:
first_line = f.readline() # 读取第一行
second_line = f.readline() # 读取第二行
print("第一行:", first_line.strip()) # .strip()去除末尾的换行符
print("第二行:", second_line.strip())
print("-" * 20)
with open('example.txt', 'r', encoding='utf-8') as f:
all_lines = f.readlines() # 读取所有行到列表中
print("所有行列表:", [line.strip() for line in all_lines])3. 写入文件
write(string)
writelines(list_of_strings)
# 写入文件
with open('new_file.txt', 'w', encoding='utf-8') as f:
f.write("这是新写入的第一行。\n")
f.write("这是第二行内容。\n")
print("内容已写入 new_file.txt")
# 追加内容到文件
with open('new_file.txt', 'a', encoding='utf-8') as f:
f.write("这是追加进来的第三行。\n")
print("内容已追加到 new_file.txt")
# 使用writelines写入多行
lines_to_write = [
"列表写入的第一行。\n",
"列表写入的第二行。\n"
]
with open('another_file.txt', 'w', encoding='utf-8') as f:
f.writelines(lines_to_write)
print("列表内容已写入 another_file.txt")4. 文件的关闭
如果你不使用
with
file_object.close()
# 不推荐但需要了解的关闭方式
f = open('temp.txt', 'w')
f.write("临时内容")
f.close() # 必须手动关闭with
说实话,
with
具体来说,
with open(...) as f:
with
__exit__
f.close()
with
try...finally
with
举个例子,如果没有
with
f = open('data.txt', 'r')
try:
content = f.read()
# 假设这里发生了一个错误,比如除零错误
result = 1 / 0
except Exception as e:
print(f"发生错误: {e}")
finally:
f.close() # 无论如何都会执行而有了
with
try:
with open('data.txt', 'r') as f:
content = f.read()
# 假设这里发生了一个错误
result = 1 / 0
except Exception as e:
print(f"发生错误: {e}")
# 文件在这里已经自动关闭了,即使发生了异常显然,
with
处理大文件,我个人经验是,千万别想着一口气把所有内容都读进来,除非你确定文件很小或者你的内存足够大到可以装下几个这样的文件。那样做,轻则程序卡顿,重则直接内存溢出(MemoryError),程序崩溃。高效处理大文件的关键在于“流式”读取,也就是一次只读取文件的一部分,处理完这部分再读取下一部分。
Python提供了几种非常实用的策略:
逐行迭代(推荐):这是最常见也是最Pythonic的方法。文件对象本身就是一个迭代器,你可以直接在
for
def process_large_file_line_by_line(filepath):
line_count = 0
with open(filepath, 'r', encoding='utf-8') as f:
for line in f: # f本身就是行迭代器
# 这里处理每一行数据
# print(f"处理行: {line.strip()}")
line_count += 1
if line_count % 100000 == 0: # 每10万行打印一次进度
print(f"已处理 {line_count} 行...")
print(f"文件 '{filepath}' 处理完毕,共 {line_count} 行。")
# 假设有一个很大的文件 'large_data.txt'
# process_large_file_line_by_line('large_data.txt')这种方式的内存占用非常小,只与当前处理的行长度有关。
分块读取(read(size)
read(size)
size
def process_large_binary_file_in_chunks(filepath, chunk_size=4096):
total_bytes_read = 0
with open(filepath, 'rb') as f: # 注意是二进制模式
while True:
chunk = f.read(chunk_size)
if not chunk: # 读取到文件末尾
break
# 这里处理读取到的chunk数据
# print(f"读取到 {len(chunk)} 字节的块")
total_bytes_read += len(chunk)
# 示例:写入到一个新文件
# with open('output_binary.bin', 'ab') as out_f:
# out_f.write(chunk)
print(f"文件 '{filepath}' 处理完毕,共读取 {total_bytes_read} 字节。")
# process_large_binary_file_in_chunks('large_image.bin')这种方法适合处理非结构化的数据流,或者当你需要精确控制每次从磁盘读取的数据量时。
使用fileinput
fileinput
这些策略的核心思想都是避免一次性加载整个文件到内存,从而有效地管理内存使用,确保程序在大文件面前依然稳定高效。
编码问题,说实话,是文件操作里最让人头疼的“隐形杀手”之一。很多时候,文件读写出了问题,程序报错
UnicodeDecodeError
UnicodeEncodeError
1. open()
encoding
这是解决编码问题的核心。在
open()
encoding
读取文件时:你需要告诉Python这个文件是用什么编码保存的,这样Python才能正确地将其中的字节序列解码成Unicode字符串。如果指定错误,就会出现
UnicodeDecodeError
# 假设 'gbk_file.txt' 是一个用GBK编码保存的文件
# 内容是:你好,世界!
try:
with open('gbk_file.txt', 'r', encoding='gbk') as f:
content = f.read()
print(f"成功读取GBK文件: {content}")
except UnicodeDecodeError as e:
print(f"读取GBK文件失败,编码错误: {e}")
# 如果用错误的编码(比如UTF-8)去读GBK文件,就会报错
try:
with open('gbk_file.txt', 'r', encoding='utf-8') as f:
content = f.read()
print(f"错误读取UTF-8文件: {content}")
except UnicodeDecodeError as e:
print(f"预期错误:尝试用UTF-8读取GBK文件导致解码失败: {e}")写入文件时:你需要告诉Python你想用什么编码来保存你的Unicode字符串到文件。如果你的字符串包含目标编码不支持的字符,或者你指定了错误的编码,可能会出现
UnicodeEncodeError
# 写入一个UTF-8编码的文件
with open('utf8_output.txt', 'w', encoding='utf-8') as f:
f.write("Hello, 世界!这是UTF-8编码的文本。")
print("UTF-8文件写入成功。")
# 尝试写入一个GBK编码的文件,但内容可能超出GBK的字符集范围
# GBK不支持某些生僻字,但对于常用汉字是没问题的
with open('gbk_output.txt', 'w', encoding='gbk') as f:
f.write("你好,Python!这是GBK编码的文本。")
print("GBK文件写入成功。")
# 如果写入的字符在GBK中不存在,且没有指定错误处理,就会报错
try:
with open('gbk_output_error.txt', 'w', encoding='gbk') as f:
# 假设这个字符 '?' 在GBK中没有对应的编码
f.write("这是一个表情符号?,GBK可能无法编码。")
except UnicodeEncodeError as e:
print(f"写入GBK文件失败,编码错误: {e}")
print("可以通过指定 errors 参数来处理,例如 errors='ignore' 或 errors='replace'")2. 默认编码和系统编码
如果你不指定
encoding
3. 错误处理:errors
当遇到无法解码或编码的字符时,
encoding
errors
'strict'
UnicodeDecodeError
UnicodeEncodeError
'ignore'
'replace'
?
�
'backslashreplace'
\xhh
\uxxxx
# 示例:使用 errors 参数
with open('gbk_output_error_handled.txt', 'w', encoding='gbk', errors='replace') as f:
f.write("这是一个表情符号?,GBK可能无法编码。但我们用替换策略。")
print("写入GBK文件,并用'replace'策略处理了编码错误。")总而言之,处理编码问题,最关键的就是明确、一致。知道你的文件是用什么编码保存的,并在
open()
以上就是python中如何读取和写入文件_Python文件读写操作指南的详细内容,更多请关注php中文网其它相关文章!
python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号