创建 ZipFile 时出现 AttributeError [英] AttributeError when creating ZipFile

查看:42
本文介绍了创建 ZipFile 时出现 AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题

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

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

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

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

这是否也是我在使用 urllib 时犯的错误,比如从直接链接中检索文件?

代码

from urllib.request import urlretrieve从操作系统导入路径从 zipfile 导入 ZipFiledownload_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() # 测试后删除

追溯

$ python downloader.py回溯(最近一次调用最后一次):文件downloader.py",第 15 行,在 <module> 中设置程序()文件downloader.py",第 11 行,在 setup_program 中zip_file = ZipFile(zip_name, "r")文件C:\Python36\lib\zipfile.py",第 1100 行,在 __init__ 中self._RealGetContents()文件C:\Python36\lib\zipfile.py",第 1163 行,在 _RealGetContentsendrec = _EndRecData(fp)文件C:\Python36\lib\zipfile.py",第 241 行,在 _EndRecDatafpin.seek(0, 2)AttributeError: 'tuple' 对象没有属性 'seek'

提前致谢,任何帮助将不胜感激.

解决方案

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

zip_name, _ = urlretrieve(download_url)

Question

I get an AttributeError: 'tuple' object has no attribute 'seek' when attempting to create a zipfile.ZipFile from a file path.

I have no idea why, the traceback doesn't make any sense in relation to my code, is this a bug in the zipfile module, or did I not set something up properly?

I followed all documentation as best as I could, to no avail.

What is wrong with what I am doing, and is there a workaround / fix for it?

And could it also be a mistake I am making with urllib in any way, as in retrieving the file from the direct link?

Code

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

Traceback

$ 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'

Thanks ahead of time, any help would be appreciated.

解决方案

urlretrieve() returns a tuple of the local filename and the headers. You should take the first item in that tuple and pass that to ZipFile instead of the tuple itself.

zip_name, _ = urlretrieve(download_url)

这篇关于创建 ZipFile 时出现 AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆