python编程之处理GB级的大型文件
一般我们采取分块处理,一次处理固定大小的块。
1 def read_in_chunks(file_obj,chunk_size): 2 """Lazy function (generator) to read a file piece by piece""" 3 while True: 4 data = file_obj.read(chunk_size) 5 if data == "": 6 break 7 yield data 8 9 10 11 file = open(file_path,"rb") 12 13 for piece in read_in_chunks(file,chunk_size): 14 process_data(piece)