tarfile doesn't work with tgz files?

draeath draeath.spamtrap at gmail.com
Sat May 16 20:33:04 CEST 2009


On 2009-05-16, Robert Dailey <rcdailey at gmail.com> wrote:
> On May 16, 11:49 am, Gary Herron <gher... at islandtraining.com> wrote:
>> 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
>
> Okay so I managed to figure out the problem.
>
> Initially, I manually downloaded the TGZ above through Chrome and then
> opened it in WinRAR and extracted the files. Everything worked fine.
>
> Then, I ran my Python script which uses urlretrieve() to download the
> file and then tarfile is used to extract it. However, urlretrieve()
> was not downloading the full file because of a bug that was corrupting
> my URL.
>
> Sorry for the confusion, and thanks to everyone for the very prompt
> help!


Just to clarify:

A tar.gz is a gzipped tar archive. It's a normal tar file, processed
into a normal gzip file.

tar.bz2 is the same, but bzip2 instead of gzip.

So, if for some reason you want to do it yourself, just decompress the
data first, then handle it like a tar file.



More information about the Python-list mailing list