Python的IO(输入/输出)操作是编程中非常基础且重要的部分,它允许程序与外部世界进行交互。本文将详细介绍Python中的文件读写操作、标准输入输出以及相关的最佳实践。
一、文件读写操作
1. 打开和关闭文件
在Python中,使用open()函数打开文件,使用close()方法关闭文件。
1 2 3 4 5 6 7 8 9
| file = open('example.txt', 'r')
content = file.read() print(content)
file.close()
|
2. 文件打开模式
| 模式 |
描述 |
| r |
只读模式(默认) |
| w |
写入模式,会覆盖现有文件 |
| a |
追加模式,在文件末尾添加内容 |
| x |
独占创建模式,如果文件已存在则报错 |
| b |
二进制模式 |
| t |
文本模式(默认) |
| + |
读写模式 |
1 2 3 4 5 6 7 8
| with open('image.jpg', 'rb') as f: data = f.read()
with open('example.txt', 'r+') as f: content = f.read() f.write('Additional content')
|
3. 使用with语句
with语句是处理文件的推荐方式,它会自动管理文件的关闭,即使出现异常也能保证文件正确关闭。
1 2 3 4 5
| with open('example.txt', 'r') as file: content = file.read() print(content)
|
4. 读取文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| with open('example.txt', 'r') as f: content = f.read() print(content)
with open('example.txt', 'r') as f: for line in f: print(line.strip())
with open('example.txt', 'r') as f: content = f.read(100) print(content)
with open('example.txt', 'r') as f: lines = f.readlines() print(lines)
|
5. 写入文件内容
1 2 3 4 5 6 7 8 9 10 11 12 13
| with open('example.txt', 'w') as f: f.write('Hello, world!\n') f.write('This is a test.\n')
with open('example.txt', 'a') as f: f.write('Additional line.\n')
lines = ['Line 1\n', 'Line 2\n', 'Line 3\n'] with open('example.txt', 'w') as f: f.writelines(lines)
|
二、标准输入输出
1. 标准输出(print)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| print('Hello, world!')
print('Hello', 'world', '!')
print('Hello', 'world', sep=', ')
print('Hello', end=' ') print('world')
name = 'Alice' age = 25 print(f'Name: {name}, Age: {age}') print('Name: {}, Age: {}'.format(name, age)) print('Name: %s, Age: %d' % (name, age))
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| name = input('Enter your name: ') print(f'Hello, {name}!')
age = int(input('Enter your age: ')) print(f'You are {age} years old.')
print('Enter multiple lines (press Ctrl+D to end):') lines = [] while True: try: line = input() lines.append(line) except EOFError: break print('You entered:') for line in lines: print(line)
|
三、二进制文件操作
1. 读取二进制文件
1 2 3 4 5 6 7 8 9
| with open('image.jpg', 'rb') as f: data = f.read() print(f'File size: {len(data)} bytes')
with open('image.jpg', 'rb') as f: header = f.read(10) print(f'Header: {header}')
|
2. 写入二进制文件
1 2 3 4 5 6 7 8 9 10 11 12 13
| data = b'Hello, binary world!' with open('binary.bin', 'wb') as f: f.write(data)
with open('source.jpg', 'rb') as src: with open('destination.jpg', 'wb') as dst: while True: chunk = src.read(1024) if not chunk: break dst.write(chunk)
|
四、文件位置操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| with open('example.txt', 'r+') as f: print(f'Current position: {f.tell()}') content = f.read(10) print(f'Read: {content}') print(f'Position after read: {f.tell()}') f.seek(0) print(f'Position after seek(0): {f.tell()}') f.seek(0, 2) print(f'Position at end: {f.tell()}')
|
五、异常处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| try: with open('non_existent_file.txt', 'r') as f: content = f.read() except FileNotFoundError: print('Error: File not found') except PermissionError: print('Error: Permission denied') except Exception as e: print(f'Error: {e}')
try: with open('protected_file.txt', 'w') as f: f.write('Test content') except Exception as e: print(f'Error writing file: {e}')
|
六、高级文件操作
1. 文件路径操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| import os import pathlib
print(f'Current directory: {os.getcwd()}')
file_path = os.path.join('data', 'example.txt') print(f'File path: {file_path}')
print(f'File exists: {os.path.exists(file_path)}')
path = pathlib.Path('data') / 'example.txt' print(f'Path: {path}') print(f'Path exists: {path.exists()}')
|
2. 文件信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import os import stat
file_path = 'example.txt' if os.path.exists(file_path): print(f'File size: {os.path.getsize(file_path)} bytes') print(f'Last modified: {os.path.getmtime(file_path)}') print(f'Is file: {os.path.isfile(file_path)}') print(f'Is directory: {os.path.isdir(file_path)}')
from pathlib import Path path = Path('example.txt') if path.exists(): print(f'File size: {path.stat().st_size} bytes') print(f'Last modified: {path.stat().st_mtime}') print(f'Is file: {path.is_file()}') print(f'Is directory: {path.is_dir()}')
|
七、综合示例
1. 文本文件处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
""" 文本文件处理示例 """
def read_file(file_path): """读取文件内容""" try: with open(file_path, 'r', encoding='utf-8') as f: return f.read() except Exception as e: print(f'Error reading file: {e}') return None
def write_file(file_path, content): """写入文件内容""" try: with open(file_path, 'w', encoding='utf-8') as f: f.write(content) return True except Exception as e: print(f'Error writing file: {e}') return False
def append_file(file_path, content): """追加文件内容""" try: with open(file_path, 'a', encoding='utf-8') as f: f.write(content) return True except Exception as e: print(f'Error appending file: {e}') return False
def count_lines(file_path): """统计文件行数""" try: with open(file_path, 'r', encoding='utf-8') as f: return sum(1 for line in f) except Exception as e: print(f'Error counting lines: {e}') return 0
def main(): test_file = 'test.txt' content = 'Hello, world!\nThis is a test.\nPython IO operations.' if write_file(test_file, content): print(f'File {test_file} created successfully.') file_content = read_file(test_file) if file_content: print(f'File content:\n{file_content}') append_content = '\nAdditional line.' if append_file(test_file, append_content): print('Content appended successfully.') file_content = read_file(test_file) if file_content: print(f'Updated file content:\n{file_content}') line_count = count_lines(test_file) print(f'Number of lines: {line_count}')
if __name__ == '__main__': main()
|
2. 二进制文件处理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
""" 二进制文件处理示例 """
def copy_file(src_path, dst_path): """复制文件""" try: with open(src_path, 'rb') as src: with open(dst_path, 'wb') as dst: while True: chunk = src.read(1024 * 1024) if not chunk: break dst.write(chunk) print(f'File copied from {src_path} to {dst_path}') return True except Exception as e: print(f'Error copying file: {e}') return False
def read_binary_file(file_path): """读取二进制文件""" try: with open(file_path, 'rb') as f: return f.read() except Exception as e: print(f'Error reading binary file: {e}') return None
def main(): test_file = 'test.bin' binary_data = b'Hello, binary world!\x00\x01\x02\x03' with open(test_file, 'wb') as f: f.write(binary_data) print(f'Binary file {test_file} created.') data = read_binary_file(test_file) if data: print(f'Read {len(data)} bytes: {data}') copy_file(test_file, 'test_copy.bin')
if __name__ == '__main__': main()
|
八、最佳实践
- 始终使用with语句:确保文件正确关闭,避免资源泄漏
- 指定编码:在处理文本文件时,明确指定编码(如utf-8)
- 异常处理:捕获并处理可能的文件操作异常
- 分块处理:对于大文件,使用分块读取和写入
- 使用pathlib:对于路径操作,推荐使用pathlib模块
- 文件权限:确保有适当的文件读写权限
- 文件路径:使用相对路径或绝对路径时要注意跨平台兼容性
- 二进制模式:处理非文本文件时使用二进制模式
九、总结
Python的IO操作提供了强大而灵活的文件处理能力,无论是文本文件还是二进制文件,都可以通过简洁的语法进行操作。通过本文的介绍,你应该已经掌握了Python中文件读写、标准输入输出以及相关的高级操作。在实际项目中,合理使用这些IO操作可以帮助你更有效地处理数据和与外部世界交互。