【问题标题】:Does Python 3 gzip closes the fileobj?Python 3 gzip 会关闭 fileobj 吗?
【发布时间】:2020-07-01 03:00:26
【问题描述】:

gzip docs for Python 3 声明

调用 GzipFile 对象的 close() 方法不会关闭 fileobj,因为您可能希望在压缩数据之后附加更多材料

这是否意味着如果我们执行以下操作,gzip 文件处理程序f_in 不会关闭

import gzip
import shutil
with gzip.open('/home/joe/file.txt.gz', 'rb') as f_in:
    with open('/home/joe/file.txt', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

如果是这样,如果多次执行这段代码会导致泄漏吗?

【问题讨论】:

    标签: python python-3.x compression gzip zlib


    【解决方案1】:

    有关 fileobj 未关闭的警告仅在打开文件并通过 fileobj= 参数将其传递给 GzipFile 时适用。当你只传递一个文件名时,GzipFile“拥有”文件句柄并且也会关闭它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-05
      • 2013-12-05
      • 2016-01-06
      • 2010-12-20
      • 2019-01-06
      • 1970-01-01
      • 2013-11-25
      相关资源
      最近更新 更多