一般我们采取分块处理,一次处理固定大小的块。
def read_in_chunks(file_obj,chunk_size):
"""Lazy function (generator) to read a file piece by piece"""
while True:
data = file_obj.read(chunk_size)
if data == "":
break
yield data file = open(file_path,"rb") for piece in read_in_chunks(file,chunk_size):
process_data(piece)