gpt4 book ai didi

python - 创建 ZipFile 时出现属性错误

转载 作者:行者123 更新时间:2023-12-01 03:02:39 30 4
gpt4 key购买 nike

问题

尝试从文件路径创建 zipfile.ZipFile 时,我收到 AttributeError: 'tuple' object has no attribute 'seek'

我不知道为什么,回溯对我的代码没有任何意义,这是 zipfile 模块中的错误,还是我没有正确设置某些内容?

我尽我所能地遵循了所有文档,但无济于事。

我正在做的事情有什么问题,有解决方法/修复吗?

这也可能是我在 urllib 上犯的一个错误,就像从直接链接检索文件一样?

代码

from urllib.request import urlretrieve
from os import path
from zipfile import ZipFile

download_url = "https://www.dropbox.com/s/obiqvrt4m53pmoz/tesseract-4.0.0-alpha.zip?dl=1"


def setup_program():
zip_name = urlretrieve(download_url)

zip_file = ZipFile(zip_name, "r")
zip_file.extractall(path.abspath("__tesseract/"))
zip_file.close()

setup_program() # REMOVE after test

回溯

$ python downloader.py
Traceback (most recent call last):
File "downloader.py", line 15, in <module>
setup_program()
File "downloader.py", line 11, in setup_program
zip_file = ZipFile(zip_name, "r")
File "C:\Python36\lib\zipfile.py", line 1100, in __init__
self._RealGetContents()
File "C:\Python36\lib\zipfile.py", line 1163, in _RealGetContents
endrec = _EndRecData(fp)
File "C:\Python36\lib\zipfile.py", line 241, in _EndRecData
fpin.seek(0, 2)
AttributeError: 'tuple' object has no attribute 'seek'

提前致谢,如有任何帮助,我们将不胜感激。

最佳答案

urlretrieve() 返回本地文件名和 header 的元组。您应该获取该元组中的第一项并将其传递给 ZipFile 而不是元组本身。

zip_name, _ = urlretrieve(download_url)

关于python - 创建 ZipFile 时出现属性错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43670450/

30 4 0
文章推荐: jquery - 将事件处理程序从 <input> 复制到
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com