with open('file.txt', 'r') as file: content = file.read() print(content)
with open('file.txt', 'r') as file: for line in file: print(line.strip())
with open('file.txt', 'r') as file: file.seek(10) # 从文件的第 10 个字节开始读取 content = file.read(20) # 读取接下来的 20 个字节 print(content)
with open('file.txt', 'r', encoding='utf-8') as file: content = file.read() print(content)
with open('file.txt', 'r', buffering=8192) as file: content = file.read() print(content)
通过设置适当的缓冲区大小,可以提高文件读取的性能。