How does one add string to tarfile in Python3

巧了我就是萌 提交于 2019-12-12 11:24:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/13988973/how-does-one-add-string-to-tarfile-in-python3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!