我有一些非常巨大的 .gz 文件 - 解压后每个文件大小在 10 到 20 GB 左右。
我需要遍历每一行,因此我正在使用标准的:
import gzip
f = gzip.open(path+myFile, 'r')
for line in f.readlines():
#(yadda yadda)
f.close()
然而,open()
和close()
命令都需要很长时间,耗费了98%的内存+ CPU。以至于程序退出并在终端上打印Killed
。也许它正在将整个提取文件加载到内存中?
我现在正在使用类似以下的内容:
from subprocess import call
f = open(path+'myfile.txt', 'w')
call(['gunzip', '-c', path+myfile], stdout=f)
#do some looping through the file
f.close()
#then delete extracted file
这个方法可行。但是是否有更简洁的方式呢?
open
而不是readlines
卡住了吗? - abarnert