Python 3 gzip 会关闭文件对象吗?

Does Python 3 gzip closes the fileobj?

gzip docs for Python 3 表示

Calling a GzipFile object’s close() method does not close fileobj, since you might wish to append more material after the compressed data

这是否意味着如果我们执行以下操作,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)

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

只有当打开文件并通过 fileobj= 参数将其传递给 GzipFile 时,才会出现有关 fileobj 未关闭的警告。当您仅传递文件名时,GzipFile“拥有”文件句柄并将关闭它。