merged_data = []
# Process the first file
with open('file1.txt', 'rb') as file1:
while True:
chunk = file1.read(1024) # Read 1MB at a time (adjust chunk size as needed)
if not chunk:
break # End of file
merged_data.append(chunk)
# Process the second file
with open('file2.txt', 'rb') as file2:
while True:
chunk = file2.read(1024) # Read 1MB at a time (adjust chunk size as needed)
if not chunk:
break # End of file
merged_data.append(chunk)
# Merge the chunks into a single variable
final_data = b''.join(merged_data)
# Now 'final_data' contains the merged content of both files
1条答案
按热度按时间ezykj2lf1#
以下是该过程的高级概述:
1.按顺序打开和读取文件:不要同时将两个文件加载到内存中,而是一个接一个地读取它们。
1.在Chunks中处理
1.关闭并释放资源
1.重复:继续阅读、处理和合并块,直到处理完整个文件。
下面是一个Python代码片段,说明了这种方法: