Frage

I have problem adding an str to a tar arhive in python. In python 2 I used such method:

fname = "archive_name"
params_src = "some arbitrarty string to be added to the archive"

params_sio = io.StringIO(params_src)
archive = tarfile.open(fname+".tgz", "w:gz")
tarinfo = tarfile.TarInfo(name="params")
tarinfo.size = len(params_src)
archive.addfile(tarinfo, params_sio)

Its essentially the same what can be found in this here. It worked well. However, going to python 3 it broke and results with the following error:

  File "./translate_report.py", line 67, in <module>
    main()
  File "./translate_report.py", line 48, in main
    archive.addfile(tarinfo, params_sio)
  File "/usr/lib/python3.2/tarfile.py", line 2111, in addfile
    copyfileobj(fileobj, self.fileobj, tarinfo.size)
  File "/usr/lib/python3.2/tarfile.py", line 276, in copyfileobj
    dst.write(buf)
  File "/usr/lib/python3.2/gzip.py", line 317, in write
    self.crc = zlib.crc32(data, self.crc) & 0xffffffff
  TypeError: 'str' does not support the buffer interface

To be honest I have trouble understanding where it comes from since I do not feed any str to tarfile module back to the point where I do construct StringIO object. I know the meanings of StringIO and str, bytes and such changed a bit from python 2 to 3 but I do not see a mistake and cannot come up with better logic to solve this task.

I create StringIO object precisely to provide buffer methods around the string I want to add to the archive. Yet it strikes me that some str does not provide it. On top of it the exception is raised around lines that seem to be responsible for checksum calculations.

Can some one please explain what I am miss-understanding or at least give an example how to add a simple str to the tar archive with out creating an intermediate file on the file-system.

War es hilfreich?

Lösung

When writing to a file, you need to encode your unicode data to bytes explicitly; StringIO objects do not do this for you, it's a text memory file. Use io.BytesIO() instead and encode:

params_sio = io.BytesIO(params_src.encode('utf8'))

Adjust your encoding to your data, of course.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top