Python 3-In-Memory-Zipfile Fehler. string-argument erwartet, hab 'bytes'

Ich habe den folgenden code zum erstellen einer in-memory-zip-Datei, das wirft einen Fehler 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)

Klar sein, dies ist nur eine Python-3 problem. Ich kann den code gut auf Python 2. Um genau zu sein bin ich mit Python 3.4.3. Die stack-trace-unter:

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'
InformationsquelleAutor pxg | 2015-08-18
Schreibe einen Kommentar