I have a problem adding str to a tar archive in python. In python 2, I used a method like this:
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)
In essence, this is what can be found here here . He worked well. However, switching to python 3, it broke and received 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, itβs hard for me to figure out where it came from, because I donβt pass the str module in the tarfile back to the point where I create the StringIO object. I know the StringIO and str values, bytes, and they have changed a bit from python 2 to 3, but I see no error and cannot come up with the best logic to solve this problem.
I am creating a StringIO object exactly to provide buffer methods around the string I want to add to the archive. But it seems to me that some str do not provide. In addition, an exception occurs around lines that appear to be responsible for checksum calculations.
Can someone explain what I am missing or at least give an example how to add a simple str to the tar archive without creating an intermediate file on the file system.