Reading tar/gz-files

Martin von Loewis loewis at informatik.hu-berlin.de
Thu Jan 6 14:44:07 EST 2000


Thomas Weholt <thomas at bibsyst.no> writes:

> Would it be possible to read out the contents of tar.gz/tgz files using
> Python? Could I, using Linux, use catch the output of a os.exec('tar tvf
> ' + filename) for instance, in a list??

You could use os.popen; this connects you to the output of tar. That
would work fine for tar tvf. See the library reference about usage of
os.popen.

If you want to get to the files as well, I recommend that you unpack
the tar file onto disk. If you absolutely want to live without
temporary files, you can use the 'O' option for receiving the file
contents through the pipe.

Alternatively, you could try to live without an external tar command,
and implement that yourself. The gzip module is already there and
provides you with an uncompressed stream. All you'd need to do is to
interpret the format of tar yourself. See /usr/include/tar.h for a
starting point.

Hope this helps,
Martin



More information about the Python-list mailing list