如何使用 Python 进行文件读写操作?

如何使用 Python 进行文件读写操作?
最新回答
鲜血染红嫁衣

2021-06-19 20:51:08

Python 文件读写操作可通过内置的 open() 函数结合不同模式实现,同时可借助 json、csv、pandas、pickle 等模块处理特定格式文件。 以下是详细方法及示例:

一、基础文件读写操作
  1. 文件读取

    读取全部内容:使用 read() 方法一次性读取文件所有内容。

    with open('example.txt', 'r') as file: content = file.read() print(content)

    逐行读取

    通过 readlines() 将内容按行存储为列表,再遍历列表。

    直接迭代文件对象(更高效,无需额外存储列表)。

    with open('example.txt', 'r') as file: # 方法1:readlines() lines = file.readlines() for line in lines: print(line.strip()) # 去除行末换行符 # 方法2:直接迭代 file.seek(0) # 重置指针到开头 for line in file: print(line.strip())
  2. 文件写入

    覆盖写入:使用模式 'w',若文件存在则清空内容,不存在则创建。

    with open('output.txt', 'w') as file: file.write("Hello, World!n") # 需手动添加换行符 file.write("This is a new line.")
  3. 文件追加

    使用模式 'a',在文件末尾添加内容,不覆盖原有数据。

    with open('output.txt', 'a') as file: file.write("nThis is an appended line.")
二、二进制文件操作
  1. 二进制读取

    使用模式 'rb' 读取二进制文件(如图片、音频等)。

    with open('example.bin', 'rb') as file: binary_data = file.read() print(binary_data) # 输出二进制数据(如 b'x48x65...')
  2. 二进制写入

    使用模式 'wb' 写入二进制数据,需通过 b'' 前缀定义二进制内容。

    with open('output.bin', 'wb') as file: binary_data = b'x48x65x6cx6cx6fx20x57x6fx72x6cx64' # "Hello World"的二进制 file.write(binary_data)
三、特定格式文件处理
  1. JSON 文件

    写入:使用 json.dump() 将 Python 对象序列化为 JSON 格式。

    读取:使用 json.load() 将 JSON 数据反序列化为 Python 对象。

    import json# 写入data = {'name': 'John', 'age': 30, 'city': 'New York'}with open('data.json', 'w') as file: json.dump(data, file)# 读取with open('data.json', 'r') as file: loaded_data = json.load(file) print(loaded_data) # 输出: {'name': 'John', 'age': 30, 'city': 'New York'}
  2. CSV 文件

    写入:使用 csv.writer 逐行写入数据,需指定 newline='' 避免空行。

    读取:使用 csv.reader 逐行读取数据为列表。

    import csv# 写入data = [['Name', 'Age', 'City'], ['John', 30, 'New York'], ['Jane', 25, 'Chicago']]with open('data.csv', 'w', newline='') as file: writer = csv.writer(file) writer.writerows(data)# 读取with open('data.csv', 'r') as file: reader = csv.reader(file) for row in reader: print(row) # 输出每行数据列表,如 ['Name', 'Age', 'City']
  3. Pandas 处理结构化数据

    写入:通过 DataFrame.to_csv() 将数据框保存为 CSV 文件。

    读取:通过 pd.read_csv() 读取 CSV 文件为数据框。

    import pandas as pd# 写入data = {'Name': ['John', 'Jane'], 'Age': [30, 25], 'City': ['New York', 'Chicago']}df = pd.DataFrame(data)df.to_csv('data_pandas.csv', index=False) # 不保存索引# 读取df_read = pd.read_csv('data_pandas.csv')print(df_read)
四、对象序列化(Pickle)
  • 序列化:使用 pickle.dump() 将 Python 对象保存为二进制文件。
  • 反序列化:使用 pickle.load() 从二进制文件恢复对象。import pickle# 序列化data = {'name': 'John', 'age': 30, 'city': 'New York'}with open('data.pkl', 'wb') as file: pickle.dump(data, file)# 反序列化with open('data.pkl', 'rb') as file: loaded_data = pickle.load(file) print(loaded_data) # 输出: {'name': 'John', 'age': 30, 'city': 'New York'}
关键注意事项
  • 文件路径:若文件不在当前目录,需指定完整路径(如 C:/path/file.txt)。
  • 异常处理:建议用 try-except 捕获 IOError 等异常,增强代码健壮性。
  • 资源管理:优先使用 with 语句自动关闭文件,避免手动调用 file.close()。
  • 编码问题:处理非文本文件时,无需指定编码;文本文件可添加 encoding='utf-8' 参数。

通过上述方法,可灵活应对不同场景下的文件操作需求。