[Tutor] zipfile error message

Steven D'Aprano steve at pearwood.info
Tue Feb 8 12:09:33 CET 2011


Eric Stevens wrote:
> Hi:
> 
> I am relatively new to Python and have been recently trying to experiment
> with its zipfile capabilities. However, everytime I try to open a zip (
> using method zipfile.ZipFile(open('zipfile.zip','r')) ) I continue to get an
> error message that states:error: unpack requires a string argument of length
> 46. Would anyone be able to help me figure out what is wrong? I am currently
> running Python 2.7 on Windows (not sure if that matters). Thank you.


When posting error messages, please copy and paste the *entire* error 
message. Do not retype it, paraphrase it, or summarize it.

But in this case, I can guess the error. You're opening the zip file in 
text mode:

open('zipfile.zip','r')

which will cause corruption of binary files. Zip files are binary and 
must be either opened in binary mode:

open('zipfile.zip','rb')


or better still, just pass the file name to ZipFile and let it open it:

zipfile.ZipFile('zipfile.zip','r')



-- 
Steven


More information about the Tutor mailing list