stringio+tarfile

Dave Angel davea at ieee.org
Thu Jul 2 22:35:59 EDT 2009


superpollo wrote:
> <div class="moz-text-flowed" style="font-family: -moz-fixed">why the 
> following does not work? can you help me correct (if possible)?
>
>       1 import tarfile
>       2 import StringIO
>       3 sf1 = StringIO.StringIO("one\n")
>       4 sf2 = StringIO.StringIO("two\n")
>       5 tf = StringIO.StringIO()
>       6 tar = tarfile.open(tf , "w")
>       7 for name in [sf1 , sf2]:
>       8     tar.add(name)
>       9 print tf
>      10 sf1.close()
>      11 sf2.close()
>      12 tf.close()
>      13 tar.close()
>
> tia
>
> </div>
>
Couldn't you have given a clue as to what doesn't work?   What version 
of Python are you using, and on what platform?  Do you get an error 
message, or are the results unreasonable? Details please.

First problem I see is all those numbers before the lines.  That's not 
valid python.

Assuming that was a transcription error, we get to some other problems.

tarfile.open() first argument is a filename, and you're passing it a 
Stringio object.  Not the same at all.  Fortunately, the Stringio should 
work as the third parameter, so you can change this line to
   tarfile.open(None, "w", tf)


Similarly tar.add() takes a filename as a parameter, and you're trying 
to pass another Stringio.  That's not a possible argument to the add() 
method at all.  Try tar.addfile()

I stopped at this point.  Maybe somebody else can help, after you fill 
in a few details.




More information about the Python-list mailing list