Python File Reading in Chunks: Complete Guide
You can chunk read a file in Python using the following methods:
- unlocked
- Get the size of something by reading it.
- Process or save the read data blocks in a loop.
- terminate()
Here is an example code that demonstrates how to read a file in chunks and write the data to a new file:
chunk_size = 1024 # 指定数据块的大小
with open('input_file.txt', 'rb') as input_file, open('output_file.txt', 'wb') as output_file:
while True:
data = input_file.read(chunk_size) # 读取数据块
if not data:
break # 如果没有数据了,结束循环
output_file.write(data) # 写入数据块到输出文件
print("文件分块读取完成")
In the code above, we open an input file input_file.txt and an output file output_file.txt. Then, within a loop, we use the read() method to read data chunks and write them to the output file. Finally, we close the file objects and print a message.