ZipExtFile 在 python3.7.0 中没有 seek 方法

ZipExtFile does not have a seek method in python3.7.0

zipfile documentation for python 3.7 声明 zipfile.ZipFile.open 方法 returns 一个 ZipExtFile 对象与 seek 方法:

With mode 'r' the file-like object (ZipExtFile) is read-only and provides the following methods: read(), readline(), readlines(), seek(), tell(), __iter__(), __next__(). These objects can operate independently of the ZipFile.

但是,当我尝试 运行 我的测试代码时:

from zipfile import ZipFile

text = b'hello world'

with ZipFile('spam.zip', 'w') as inzip:
    with inzip.open('eggs.txt', 'w') as infile:
        infile.write(text)

with ZipFile('spam.zip', 'r') as myzip:
    with myzip.open('eggs.txt', 'r') as myfile:
        print(myfile.read())
        myfile.seek(0)
        print(myfile.read())

然后我收到此错误消息:

$ python3.7 zip_test.py
b'hello world'
Traceback (most recent call last):
  File "zip_test.py", line 13, in <module>
    myfile.seek(0)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 1025, in seek
    self._fileobj.seek(self._orig_compress_start)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/zipfile.py", line 704, in seek
    if self.writing():
AttributeError: '_SharedFile' object has no attribute 'writing'

有谁知道我做错了什么吗?

其他阅读

这里有一些 SO 问题,人们似乎 运行 正在关注这个问题,尽管可能是年长的 python 口译员:

更新

我是运行宁python版本3.7.0.

您需要升级到更新的 Python 3.7.x 版本,您 运行 进入 confirmed and fixed bugs in the ZipFile.seek() implementation, see issue #34035.

这些修复已在 3.7.1rc1 release 中完成,但我建议尽可能直接升级到 3.7.2。