AttributeError when creating ZipFile

送分小仙女□ 提交于 2019-12-24 10:04:21

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/43670450/attributeerror-when-creating-zipfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!