stringio+tarfile

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Jul 2 21:28:37 EDT 2009


En Thu, 02 Jul 2009 17:57:49 -0300, superpollo <user at example.net> escribió:

> why the following does not work? can you help me correct (if possible)?

Explain "does not work" please. Does it raise an exception? Which one?
Please post the complete traceback. You don't get the expected result?
Tell us what did you expect and what you actually get. If you provide this
information, it's a lot easier for people to look at your problem and
eventually find a solution.

Fortunately my crystall ball is back from the repair shop and I can guess
that you want to create a tar file in memory, and add some content from a
memory buffer too, right?
I didn't run your code but I can see two problems:

>        5 tf = StringIO.StringIO()
>        6 tar = tarfile.open(tf , "w")

The first argument to tarfile.open is the *name* of the file to
open/create. In this case you don't have a real file, but a StringIO
object; use the fileobj argument instead: tarfile.open(fileobj=tf,
mode="w")
See http://docs.python.org/library/tarfile.html#tarfile.open

>        7 for name in [sf1 , sf2]:
>        8     tar.add(name)

The add method takes the *name* of a file to add to the archive. Again,
you don't have a real file to add; try the addfile method instead.
See http://docs.python.org/library/tarfile.html#tarfile.TarFile.add


-- 
Gabriel Genellina




More information about the Python-list mailing list