摘要:open(File, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
open是Python中用于文件操作的核心函数,它提供了读写文件的能力,是处理文件输入输出的基础。
open(File, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)# 读取文件with open('example.txt', 'r', encoding='utf-8') as f:content = f.readprint(content)# 写入文件with open('output.txt', 'w', encoding='utf-8') as f:f.write('Hello, World!')# 读写模式with open('data.txt', 'r+') as f: # 读写(文件必须存在)content = f.readf.write('\nNew content')with open('data.txt', 'w+') as f: # 读写(清空文件)f.write('New content')f.seek(0)content = f.readwith open('data.txt', 'a+') as f: # 读写(追加模式)f.write('\nAppended content')f.seek(0)content = f.read# 处理二进制文件with open('image.jpg', 'rb') as f: # 二进制读取data = f.readwith open('copy.jpg', 'wb') as f: # 二进制写入f.write(data)# 文本+二进制模式with open('data.bin', 'r+b') as f: # 二进制读写existing_data = f.readf.write(b'\x00\x01\x02')# 写入时明确指定编码with open('output.txt', 'w', encoding='utf-8') as f:f.write('你好,世界!')# 读取时处理编码错误with open('file.txt', 'r', encoding='utf-8', errors='ignore') as f:content = f.read # 忽略无法解码的字符with open('file.txt', 'r', encoding='utf-8', errors='replace') as f:content = f.read # 用替换字符代替无法解码的字符with open('example.txt', 'r') as f:# 读取全部内容content = f.read# 重置文件指针f.seek(0)# 逐行读取lines = f.readlines# 重置文件指针f.seek(0)# 逐行迭代(内存友好)for line in f:print(line.strip)# 写入字符串with open('output.txt', 'w') as f:f.write('Line 1\n')f.write('Line 2\n')# 写入多行lines = ['Line 1\n', 'Line 2\n', 'Line 3\n']with open('output.txt', 'w') as f:f.writelines(lines)# 使用print重定向with open('output.txt', 'w') as f:print('Line 1', file=f)print('Line 2', file=f)# 传统方式(需要手动关闭)f = open('file.txt', 'r')try:content = f.readfinally:f.close # 必须手动关闭# 现代方式(自动关闭)with open('file.txt', 'r') as f:content = f.read# 文件自动关闭# 同时读写多个文件with open('source.txt', 'r') as source, open('dest.txt', 'w') as dest:content = source.readdest.write(content.upper)# Python 3.10+ 支持括号多行with (open('file1.txt', 'r') as f1,open('file2.txt', 'r') as f2,open('merged.txt', 'w') as out):out.write(f1.read + f2.read)with open('data.txt', 'r+') as f:# 获取当前位置position = f.tellprint(f'当前位置: {position}')# 读取前10字节data = f.read(10)# 移动到文件末尾f.seek(0, 2) # 2表示从文件末尾f.write('\nAppended text')# 移动到文件开头f.seek(0)content = f.read# 无缓冲(立即写入)with open('log.txt', 'w', buffering=0) as f:f.write('立即写入\n')# 行缓冲with open('log.txt', 'w', buffering=1) as f:f.write('行缓冲\n') # 遇到换行符时刷新# 块缓冲(默认,通常4096或8192字节)with open('data.bin', 'wb', buffering=4096) as f:f.write(b'x' * 5000) # 达到4096字节时刷新import osdef custom_opener(file, flags):# 自定义打开逻辑return os.open(file, flags, 0o644) # 设置文件权限with open('custom.txt', 'w', opener=custom_opener) as f:f.write('使用自定义打开器创建的文件')def read_config(config_path):config = {}try:with open(config_path, 'r', encoding='utf-8') as f:for line in f:line = line.stripif line and not line.startswith('#'):key, value = line.split('=', 1)config[key.strip] = value.stripexcept FileNotFoundError:print(f"配置文件 {config_path} 不存在")return configdef tail_file(file_path, n=10):"""读取文件最后n行"""with open(file_path, 'rb') as f:# 移动到文件末尾f.seek(0, 2)file_size = f.telllines = buffer = bytearraypos = file_sizewhile len(lines) 0:# 每次读取1KBread_size = min(1024, pos)pos -= read_sizef.seek(pos)chunk = f.read(read_size)buffer.extend(chunk)# 处理换行符while buffer:try:last_newline = buffer.rindex(b'\n')except ValueError:breakline = buffer[last_newline+1:].Decodeif line.strip:lines.append(line)buffer = buffer[:last_newline]if len(lines) >= n:breakreturn list(reversed(lines[-n:]))def process_large_file(input_path, output_path, chunk_size=8192):"""分块处理大文件"""with open(input_path, 'rb') as infile, open(output_path, 'wb') as outfile:while True:chunk = infile.read(chunk_size)if not chunk:break# 处理块数据processed_chunk = chunk.upper # 示例处理outfile.write(processed_chunk)import osfile_path = 'nonexistent.txt'# 检查文件是否存在if not os.path.exists(file_path):print("文件不存在")else:with open(file_path, 'r') as f:content = f.read# 或者使用try-excepttry:with open(file_path, 'r') as f:content = f.readexcept FileNotFoundError:print("文件不存在")except PermissionError:print("没有权限访问文件")# 对于大文件,避免一次性读取def count_lines(file_path):"""统计文件行数(内存友好)"""count = 0with open(file_path, 'r', encoding='utf-8') as f:for line in f:count += 1return countimport os# 使用os.path处理路径file_path = os.path.join('directory', 'subdir', 'file.txt')# 使用pathlib(Python 3.4+)from pathlib import Pathfile_path = Path('directory') / 'subdir' / 'file.txt'with open(file_path, 'r') as f:content = f.read# 健壮的文件读取函数def safe_read_file(file_path, encoding='utf-8', default=None):"""安全读取文件内容"""try:with open(file_path, 'r', encoding=encoding) as f:return f.readexcept FileNotFoundError:print(f"文件不存在: {file_path}")return defaultexcept PermissionError:print(f"没有权限读取文件: {file_path}")return defaultexcept UnicodeDecodeError:print(f"编码错误: {file_path}")return default# 使用示例content = safe_read_file('important.txt', default='')if content:process_content(content)open函数是Python文件操作的基础,掌握它的各种用法和最佳实践对于任何Python开发者都至关重要。
来源:琢磨先生起飞吧