开发者

create a tar file in a string using python

开发者 https://www.devze.com 2023-01-13 20:49 出处:网络
I need to generate a tar file but as a string in memory rather than as an actual file. What I have as input is a single filename and a string containing the assosiated contents. I\'m looking for a pyt

I need to generate a tar file but as a string in memory rather than as an actual file. What I have as input is a single filename and a string containing the assosiated contents. I'm looking for a pyt开发者_开发百科hon lib I can use and avoid having to role my own.


A little more work found these functions but using a memory steam object seems a little... inelegant. And making it accept input from strings looks like even more... inelegant. OTOH it works. I assume, as most of it is new to me. Anyone see any bugs in it?


Use tarfile in conjunction with cStringIO:

c = cStringIO.StringIO()
t = tarfile.open(mode='w', fileobj=c)
# here: do your work on t, then...:
s = c.getvalue()   # extract the bytestring you need
0

精彩评论

暂无评论...
验证码 换一张
取 消