ZipExtFile在python3.7中没有seek方法

时间:2019-02-26 13:01:11

标签: python zipfile python-3.7

zipfile documentation for python 3.7指出,zipfile.ZipFile.open方法返回带有ZipExtFile方法的seek对象:

  

在模式“ r”下,类似文件的对象(ZipExtFile)是只读的,并提供以下方法:read()readline()readlines(),{{ 1}},seek()tell()__iter__()。这些对象可以独立于__next__()进行操作。

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

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())

有人知道我在做什么错吗?

其他阅读内容

尽管有些旧的python解释器,这里还是有一些SO问题,人们似乎会遇到此问题:

更新

我正在运行python版本$ 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'

1 个答案:

答案 0 :(得分:1)

您需要升级到更新的Python 3.7.x版本,遇到confirmed and fixed bugs in the ZipFile.seek() implementation,请参见issue #34035

这些修复程序已放入3.7.1rc1 release中,但我建议尽可能直接转到3.7.2。