stringio+tarfile (or better... zipfile)

superpollo user at example.net
Fri Jul 3 16:14:22 EDT 2009


following the excellent suggestions received, i tried to adapt the 
problem to zipfile, and i wrote this:

$ cat zip001.py
import sys
import zipfile
import StringIO

nb1 = "first.txt", StringIO.StringIO("one one\n")
nb2 = "second.txt", StringIO.StringIO("two\n")
zb = StringIO.StringIO()

zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED)

for name , buffer in [nb1 , nb2]:
     zip.writestr(name, buffer.getvalue()*1000)
zip.close()

sys.stdout.write(zb.getvalue())
$ python zip001.py > zip001.out
$ unzip -l zip001.out
Archive:  zip001.out
   Length     Date   Time    Name
  --------    ----   ----    ----
      8000  07-03-09 22:07   first.txt
      4000  07-03-09 22:07   second.txt
  --------                   -------
     12000                   2 files
$

it seems to me thaz zipfile has a simpler usability... any comments?

thanks again and bye bye



More information about the Python-list mailing list