Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 In Memory Zipfile Error. string argument expected, got 'bytes'

I have the following code to create an in memory zip file that throws an error running in Python 3.

from io import StringIO from pprint import pprint import zipfile   in_memory_data = StringIO() in_memory_zip = zipfile.ZipFile(     in_memory_data, "w", zipfile.ZIP_DEFLATED, False) in_memory_zip.debug = 3  filename_in_zip = 'test_filename.txt' file_contents = 'asdf'  in_memory_zip.writestr(filename_in_zip, file_contents) 

To be clear this is only a Python 3 problem. I can run the code fine on Python 2. To be exact I'm using Python 3.4.3. The stack trace is below:

Traceback (most recent call last):   File "in_memory_zip_debug.py", line 14, in <module>     in_memory_zip.writestr(filename_in_zip, file_contents)   File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/zipfile.py", line 1453, in writestr     self.fp.write(zinfo.FileHeader(zip64)) TypeError: string argument expected, got 'bytes' Exception ignored in: <bound method ZipFile.__del__ of <zipfile.ZipFile object at 0x1006e1ef0>> Traceback (most recent call last):   File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/zipfile.py", line 1466, in __del__     self.close()   File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/zipfile.py", line 1573, in close     self.fp.write(endrec) TypeError: string argument expected, got 'bytes' 
like image 425
pxg Avatar asked Aug 18 '15 14:08

pxg


1 Answers

ZipFile writes its data as bytes, not strings. This means you'll have to use BytesIO instead of StringIO on Python 3.

The distinction between bytes and strings is new in Python 3. The six compatibility library has a BytesIO class for Python 2 if you want your program to be compatible with both.

like image 61
Wander Nauta Avatar answered Sep 19 '22 17:09

Wander Nauta