python编程之处理GB级的大型文件

一般我们采取分块处理,一次处理固定大小的块。

 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)
上一篇:iOS进阶


下一篇:Python网络编程——处理套接字错误