tarfile doesn't work with tgz files?

Gary Herron gherron at islandtraining.com
Sat May 16 12:49:36 EDT 2009

Robert Dailey wrote:
> Hi,
> I'm not a big expert on the tarfile component, but I assumed that .tgz
> files were short for .tar.gz and the format was the same. 

That's correct.

> When I try
> to extract a .tgz file using tarfile in Python 3.0 on Windows, I get
> the following error:
>   File "C:\Python30\lib\tarfile.py", line 1630, in open
>     raise ReadError("file could not be opened successfully")
> tarfile.ReadError: file could not be opened successfully
> I'm assuming this is because the .tgz format is not what I think it is
> and is not supported.

Not true.  It's supported.
>  How can I extract .tgz files using Python 3.0?
> Note that I probably can't depend on third party python modules, since
> I have yet to find many that support Python 3.0.
> Thanks.

It's hard to debug your code when you don't show us your code,  so I 
won't try.

I can however supply a bit of working code:

 >>> import tarfile
 >>> f=tarfile.open('f.tgz')
 >>> f.list()
-rw-r--r-- gherron/gherron        703 2009-03-14 15:59:46 t.py
-rw-r--r-- gherron/gherron        566 2009-05-15 09:13:08 note-bzr-hg
-rw-r--r-- gherron/gherron      11563 2009-04-24 09:33:12 .emacs

So the module correctly reads the tar-gzip file, and finds its contents.

Now, what is it you were trying to do?

Gary Herron

More information about the Python-list mailing list