文件操作的第一步是正确打开与关闭文件。Python内置的open()函数是核心工具,其基本语法为:
open(file_path, mode='r', encoding=None)
mode指定操作模式(如'r'只读、'w'写入(清空原内容)、'a'追加、'b'二进制模式、't'文本模式(默认));encoding用于文本文件的字符编码(推荐'utf-8')。with语句可自动管理文件资源,即使在操作中出现异常,文件也会被正确关闭,避免资源泄露。# 读取文件(自动关闭)
with open('example.txt', 'r', encoding='utf-8') as file:
content = file.read()
print(content)
# 写入文件(自动关闭)
with open('output.txt', 'w', encoding='utf-8') as file:
file.write("Hello, Python!\n")
对于大文件,避免一次性加载全部内容是提升性能的核心。以下是三种常用的高效读取方法:
for line in file循环,逐行处理文件内容,内存占用极低。with open('large_file.txt', 'r', encoding='utf-8') as file:
for line in file:
print(line.strip()) # 去除行尾换行符
yield关键字创建生成器函数,按需读取文件内容,适合处理超大型文件。def read_large_file(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
yield line.strip()
for line in read_large_file('huge_file.txt'):
process(line) # 自定义处理逻辑
readlines()方法将文件内容按行存储为列表,适合需要随机访问行数据的场景(注意:仍需预留足够内存)。with open('data.txt', 'r', encoding='utf-8') as file:
lines = file.readlines() # 返回列表,每行一个元素
for i, line in enumerate(lines[:10]): # 仅处理前10行
print(f"Line {i}: {line.strip()}")
写入文件时,可根据需求选择不同的方法,灵活控制内容格式:
write(string)方法,需手动添加换行符(\n)。with open('log.txt', 'a', encoding='utf-8') as file:
file.write("Error: File not found.\n") # 添加换行符
writelines(list)方法,将字符串列表中的每个元素写入文件(不会自动添加换行符,需提前处理)。lines = ["First line\n", "Second line\n", "Third line\n"]
with open('output.txt', 'w', encoding='utf-8') as file:
file.writelines(lines)
print函数:通过file参数将内容写入文件,可自动添加换行符(end='\n'),更符合日常打印习惯。with open('messages.txt', 'w', encoding='utf-8') as file:
print("Message 1", file=file)
print("Message 2", file=file)
对于图片、音频、视频等二进制文件,需使用'rb'(读取)或'wb'(写入)模式,避免编码转换带来的性能损耗。
# 复制图片(二进制模式)
with open('original.png', 'rb') as src_file:
binary_data = src_file.read()
with open('copy.png', 'wb') as dest_file:
dest_file.write(binary_data)
文件指针用于标记当前读写位置,通过tell()和seek()方法可实现精准控制:
tell():返回文件指针的当前位置(字节数)。seek(offset, whence):移动文件指针到指定位置。
offset:偏移量(字节数);whence:参考位置(0=文件开头、1=当前位置、2=文件结尾)。with open('large_file.bin', 'rb') as file:
file.seek(500) # 移动到第500字节处
chunk = file.read(100) # 读取100字节
print(chunk)
open()的buffering参数调整缓冲区大小(默认4096字节),增大缓冲区可减少I/O操作次数。with open('large_file.txt', 'r', buffering=8192) as file: # 缓冲区8KB
content = file.read()
mmap模块将文件映射到内存,像操作内存一样操作文件,大幅提升大文件读取速度。import mmap
with open('huge_file.txt', 'r+b') as file:
mmapped_file = mmap.mmap(file.fileno(), 0) # 映射整个文件
print(mmapped_file[:100].decode('utf-8')) # 读取前100字节
mmapped_file.close()
concurrent.futures.ThreadPoolExecutor(多线程)或asyncio(异步)并发处理,提升IO密集型任务的效率。import concurrent.futures
def read_chunk(file_path, start, size):
with open(file_path, 'r', encoding='utf-8') as file:
file.seek(start)
return file.read(size)
file_path = 'large_file.txt'
file_size = os.path.getsize(file_path)
chunk_size = file_size // 4
with concurrent.futures.ThreadPoolExecutor() as executor:
futures = [executor.submit(read_chunk, file_path, i * chunk_size, chunk_size)
for i in range(4)]
results = [future.result() for future in concurrent.futures.as_completed(futures)]
文件操作中常见异常包括FileNotFoundError(文件不存在)、IOError(读写错误)、PermissionError(权限不足)。使用try...except结构可捕获并处理这些异常,避免程序崩溃。
try:
with open('non_existent.txt', 'r', encoding='utf-8') as file:
content = file.read()
except FileNotFoundError:
print("错误:文件不存在!")
except IOError as e:
print(f"读写错误:{e}")
except PermissionError:
print("错误:没有文件访问权限!")
os.rename()重命名文件,shutil.move()移动文件到指定目录。import os
import shutil
os.rename('old_name.txt', 'new_name.txt') # 重命名
shutil.move('file.txt', '/path/to/destination/') # 移动文件
os.remove()删除文件(需确认文件存在且无权限问题)。import os
if os.path.exists('temp.txt'):
os.remove('temp.txt')
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。