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)

 

posted @ 2015-06-10 14:15  foo__hack  阅读(481)  评论(0编辑  收藏  举报