在tarfile的官方 python 文档中,我没有看到使用创建的 tarfile
tarfile.open('example.tar', 'r:*')
一旦你不再需要它就应该关闭它。
在其他一些示例中(例如此处),您经常会看到未关闭的 tar 文件。
如果我在某些函数中更频繁地打开同一个 tar 文件而不总是关闭它,我会遇到问题吗?
def example(name):
f = tarfile.open(name, 'r:*')
# do some other stuff
# not closing the tarfile object
return result
example('example.tar')
example('example.tar')
在 'r' 而不是 'w' 模式下打开 tarfile 是否有区别(明显的读/写除外)?
因为在示例中,如果使用写入模式“w”打开 tar 文件,他们总是关闭它?