【发布时间】:2023-03-15 03:26:02
【问题描述】:
使用 Python 3.5
我正在解压缩一个 gzip 文件,写入另一个文件。在查看内存不足问题后,我在 gzip 模块的文档中找到了一个示例:
import gzip
import shutil
with open('/home/joe/file.txt', 'rb') as f_in:
with gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
这会压缩,我想解压缩,所以我认为我可以反转模式,给
with open(unzipped_file, 'wb') as f_out, gzip.open(zipped_file, 'rb') as f_in:
shutil.copyfileobj(f_in, f_out)
我的问题是,为什么我会遇到以下问题:
with gzip.open(zipped_file, 'rb') as zin, open(unzipped_file, 'wb') as wout:
wout.write(zin.read())
要么我压倒了最后一根稻草,要么我天真地认为文件会像生成器一样运行并流式处理解压缩过程,占用很少的内存。这两种方法应该等价吗?
【问题讨论】:
-
我建议你看一下
shutil.copyfileObj方法的代码。 -
"我天真地认为文件会像生成器一样工作" -- 就是这样。你是在自问自答。为了证明,请尝试
print(type(zin.read())获取一些较小的文件。
标签: python python-3.x gzip generator shutil