Ho il seguente codice per creare un file zip nella memoria che genera un errore in esecuzione in Python 3.Errore di Zip in memoria di Python 3. argomento stringa previsto, ha 'byte'
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)
Per essere chiari questo è solo un problema di Python 3. Posso eseguire il codice bene su Python 2. Per la precisione sto usando Python 3.4.3. L'analisi dello stack è inferiore:
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'
Questo è confuso perché il messaggio di errore dell'OP implica il problema esattamente opposto. Fornisce un valore di stringa poiché è ciò che il messaggio di errore indica è il tipo di argomento previsto. –