[Tutor] How to read content in a tar file with tarfile module
Dave Kuhlman
dkuhlman at rexx.com
Tue Oct 10 22:52:46 CEST 2006
On Tue, Oct 10, 2006 at 01:19:20PM -0600, Hugo Gonz?lez Monteverde wrote:
> Magnus Wirstr?m wrote:
> > I have written a app that makes a tar file and all works well... Now i
> > want to expand that app so it can read read the tar and give me the
> > contents of the tar file. How is the best way to do this ? I can't find
> > a "listdir" like function in tarfile. Can anyone point me in the right
> > direction?
>
> Hi, is this what you're looking for?? (tarfile Object docs)
>
> getmembers( )
>
> Return the members of the archive as a list of TarInfo objects. The list
> has the same order as the members in the archive.
>
> getnames( )
>
> Return the members as a list of their names. It has the same order as
> the list returned by getmembers().
>
Note also the presence of a "next()" method in TarFile objects.
That (along with an "__iter__" method: see Lib/tarfile.py in the
source) is your clue that each TarFile instance is an iterator. It
iterates over its TarInfo members. So, you can do the following:
In [11]: f = tf.open('tmp.tar.gz', 'r:gz')
In [12]: for x in f:
....: print x.name
See:
http://docs.python.org/lib/module-tarfile.html
http://docs.python.org/lib/tarfile-objects.html
http://docs.python.org/lib/tar-examples.html
Note that the TarFile object implementation of the iterator
protocol is slightly non-standard. next() returns None when
exhausted, whereas the iterator protocol specifies that it should
throw a StopIteration exception. See:
http://docs.python.org/lib/typeiter.html
Dave
--
Dave Kuhlman
http://www.rexx.com/~dkuhlman
More information about the Tutor
mailing list