BadZipfile "file is not a zip file"

MRAB google at
Fri Jan 9 02:02:44 CET 2009

webcomm wrote:
> The error...
>>>> file = zipfile.ZipFile('', "r")
> Traceback (most recent call last):
>   File "<pyshell#23>", line 1, in <module>
>     file = zipfile.ZipFile('', "r")
>   File "C:\Python25\lib\", line 346, in __init__
>     self._GetContents()
>   File "C:\Python25\lib\", line 366, in _GetContents
>     self._RealGetContents()
>   File "C:\Python25\lib\", line 378, in _RealGetContents
>     raise BadZipfile, "File is not a zip file"
> BadZipfile: File is not a zip file
> When I look at in Windows, it appears to be a valid zip
> file.  I am able to uncompress it in Windows XP, and can also
> uncompress it with 7-Zip.  It looks like zipfile is not able to read a
> "table of contents" in the zip file.  That's not a concept I'm
> familiar with.
> is created in this script...
>     decoded = base64.b64decode(datum)
>     f = open('', 'wb')
>     f.write(decoded)
>     f.close()
>     file = zipfile.ZipFile('', "r")
> datum is a base64 encoded zip file.  Again, I am able to open
> as if it's a valid zip file.  Maybe there is something wrong with the
> approach I've taken to writing the data to  I'm not sure if
> it matters, but the zipped data is Unicode.
> What would cause a zip file to not have a table of contents?  Is there
> some way I can add a table of contents to a zip file using python?
> Maybe there is some more fundamental problem with the data that is
> making it seem like there is no table of contents?
You're just creating a file called "". That doesn't make it a 
zip file. A zip file has a specific format. If the file doesn't have 
that format then the zipfile module will complain.

More information about the Python-list mailing list