python3中实现tar_如何在Python3中将字符串添加到tarfile

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

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值