gatoatigrado gatoatigrado - 1 year ago 139
Python Question

python write string directly to tarfile

Is there a way to write a string directly to a tarfile? From it looks like only files already written to the file system can be added.

Answer Source

I would say it's possible, by playing with TarInfo e TarFile.addfile passing a StringIO as a fileobject.

Very rough, but works

import tarfile
import StringIO

tar = tarfile.TarFile("test.tar","w")

string = StringIO.StringIO()
info = tarfile.TarInfo(name="foo")
tar.addfile(tarinfo=info, fileobj=string)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download