Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

stringio+tarfile

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


user at example

Jul 2, 2009, 1:57 PM

Post #1 of 10 (306 views)
Permalink
stringio+tarfile

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
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Jul 2, 2009, 6:28 PM

Post #2 of 10 (295 views)
Permalink
Re: stringio+tarfile [In reply to]

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

--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Jul 2, 2009, 7:35 PM

Post #3 of 10 (295 views)
Permalink
Re: stringio+tarfile [In reply to]

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.

--
http://mail.python.org/mailman/listinfo/python-list


user at example

Jul 3, 2009, 4:21 AM

Post #4 of 10 (291 views)
Permalink
Re: stringio+tarfile [In reply to]

> First problem I see is all those numbers before the lines. That's not
> valid python.
>
> Assuming that was a transcription error

not so. i intentionally add linenumbers to facilitare reference to the
code, but if it is a nuisance i will not include them anymore.

thanks
--
http://mail.python.org/mailman/listinfo/python-list


user at example

Jul 3, 2009, 4:29 AM

Post #5 of 10 (291 views)
Permalink
Re: stringio+tarfile [In reply to]

thanks to the guys who bothered to answer me even using their chrystal
ball ;-)

i'll try to be more specific.

yes: i want to create a tar file in memory, and add some content from a
memory buffer...

my platform:

$ uname -a
Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux
$ python -V
Python 2.3.4

following some suggestions i modified the code:

$ cat tar001.py
import tarfile
import StringIO
sfo1 = StringIO.StringIO("one\n")
sfo2 = StringIO.StringIO("two\n")
tfo = StringIO.StringIO()
tar = tarfile.open(fileobj=tfo , mode="w")
ti = tar.gettarinfo(fileobj=tfo)
for sfo in [sfo1 , sfo2]:
tar.addfile(fileobj=sfo , tarinfo=ti)
print tfo

and that's what i get:

$ python tar001.py > tar001.out
Traceback (most recent call last):
File "tar001.py", line 7, in ?
ti = tar.gettarinfo(fileobj=tfo)
File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo
name = fileobj.name
AttributeError: StringIO instance has no attribute 'name'

can you help?

TIA

ps: i'd also like that the tar file has names for the buffers, so than
once output the file can be untarred with /bin/tar into regular files...
--
http://mail.python.org/mailman/listinfo/python-list


__peter__ at web

Jul 3, 2009, 5:12 AM

Post #6 of 10 (291 views)
Permalink
Re: stringio+tarfile [In reply to]

superpollo wrote:

> thanks to the guys who bothered to answer me even using their chrystal
> ball ;-)
>
> i'll try to be more specific.
>
> yes: i want to create a tar file in memory, and add some content from a
> memory buffer...
>
> my platform:
>
> $ uname -a
> Linux fisso 2.4.24 #1 Thu Feb 12 19:49:02 CET 2004 i686 GNU/Linux
> $ python -V
> Python 2.3.4
>
> following some suggestions i modified the code:
>
> $ cat tar001.py
> import tarfile
> import StringIO
> sfo1 = StringIO.StringIO("one\n")
> sfo2 = StringIO.StringIO("two\n")
> tfo = StringIO.StringIO()
> tar = tarfile.open(fileobj=tfo , mode="w")
> ti = tar.gettarinfo(fileobj=tfo)

gettarinfo() expects a real file, not a file-like object.
You have to create your TarInfo manually.

> for sfo in [sfo1 , sfo2]:
> tar.addfile(fileobj=sfo , tarinfo=ti)
> print tfo
>
> and that's what i get:
>
> $ python tar001.py > tar001.out
> Traceback (most recent call last):
> File "tar001.py", line 7, in ?
> ti = tar.gettarinfo(fileobj=tfo)
> File "/usr/lib/python2.3/tarfile.py", line 1060, in gettarinfo
> name = fileobj.name
> AttributeError: StringIO instance has no attribute 'name'
>
> can you help?

I recommend that you have a look into the tarfile module's source code.

The following seems to work:

import sys
import time
import tarfile
import StringIO

sf1 = "first.txt", StringIO.StringIO("one one\n")
sf2 = "second.txt", StringIO.StringIO("two\n")
tf = StringIO.StringIO()

tar = tarfile.open(fileobj=tf , mode="w")

mtime = time.time()
for name, f in [sf1 , sf2]:
ti = tarfile.TarInfo(name)
ti.size = f.len
ti.mtime = mtime
# add more attributes as needed
tar.addfile(ti, f)

sys.stdout.write(tf.getvalue())

Peter

--
http://mail.python.org/mailman/listinfo/python-list


lie.1296 at gmail

Jul 3, 2009, 6:16 AM

Post #7 of 10 (290 views)
Permalink
Re: stringio+tarfile [In reply to]

superpollo wrote:
>> First problem I see is all those numbers before the lines. That's not
>> valid python.
>>
>> Assuming that was a transcription error
>
> not so. i intentionally add linenumbers to facilitare reference to the
> code, but if it is a nuisance i will not include them anymore.

The line numbering makes it impossible for others to copy and paste to
the interactive interpreter to see the problem code running, and makes
it hard to copy and paste into a script file. For code reference,
quoting the problematic code is usually much easier.
--
http://mail.python.org/mailman/listinfo/python-list


user at example

Jul 3, 2009, 10:42 AM

Post #8 of 10 (288 views)
Permalink
Re: stringio+tarfile [In reply to]

Peter Otten wrote:

> gettarinfo() expects a real file, not a file-like object.
> You have to create your TarInfo manually.

ok... which attributes are mandatory, and which optional?


> I recommend that you have a look into the tarfile module's source code.
>

i will try... but:


$ cat /usr/lib/python2.3/tarfile.py | wc -l
1938

wow! it'll take some time ;-)


> The following seems to work:
>
> import sys
> import time
> import tarfile
> import StringIO
>
> sf1 = "first.txt", StringIO.StringIO("one one\n")
> sf2 = "second.txt", StringIO.StringIO("two\n")
> tf = StringIO.StringIO()
>
> tar = tarfile.open(fileobj=tf , mode="w")
>
> mtime = time.time()
> for name, f in [sf1 , sf2]:
> ti = tarfile.TarInfo(name)
> ti.size = f.len
> ti.mtime = mtime
> # add more attributes as needed
> tar.addfile(ti, f)
>
> sys.stdout.write(tf.getvalue())
>
> Peter
>

much obliged mr otten

bye
--
http://mail.python.org/mailman/listinfo/python-list


user at example

Jul 3, 2009, 1:14 PM

Post #9 of 10 (288 views)
Permalink
Re: stringio+tarfile (or better... zipfile) [In reply to]

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
--
http://mail.python.org/mailman/listinfo/python-list


gagsl-py2 at yahoo

Jul 3, 2009, 8:50 PM

Post #10 of 10 (279 views)
Permalink
Re: stringio+tarfile (or better... zipfile) [In reply to]

En Fri, 03 Jul 2009 17:14:22 -0300, superpollo <user[at]example.net> escribió:

> following the excellent suggestions received, i tried to adapt the
> problem to zipfile, and i wrote this:
>
> zip = zipfile.ZipFile(zb , "w" , zipfile.ZIP_DEFLATED)
> for name , buffer in [nb1 , nb2]:
> zip.writestr(name, buffer.getvalue()*1000)
> zip.close()
>
> it seems to me thaz zipfile has a simpler usability... any comments?

Maybe - but a zip and a tar file are not the same thing.

Now, if you want to say that there should be a common interfase to handle
all those file formats like tar,zip,bz2: Yes, I agree.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact lists@gossamer-threads.com
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.