python - ZipExtFile 在 python3.7.0 中没有 seek 方法
问题描述
python 3.7的zipfile 文档指出该zipfile.ZipFile.open
方法返回一个ZipExtFile
带有seek
方法的对象:
在模式 'r' 下,类文件对象 (
ZipExtFile
) 是只读的,并提供以下方法:read()
,readline()
,readlines()
,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())
然后我收到此错误消息:
$ 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 版本,您在实现中遇到了已确认和修复的错误ZipFile.seek()
,请参阅问题 #34035。
这些修复程序已在3.7.1rc1 版本中发布,但如果可能,我建议直接使用 3.7.2。