tarfile doesn't work with tgz files?

Robert Dailey rcdailey at gmail.com
Sat May 16 12:55:59 EDT 2009


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

I wasn't sure if my code was relevant, since there really isn't much
to show. Here is the convenience function I've created for extracting
the contents of a tar.gz or tgz file on Windows:

def untar( file, dst ):
   print( file )
   print( dst )
   tarball = tarfile.open( file )
   tarball.extractall( dst )
   tarball.close()

The respective results of the 2 print statements above are:

D:\IT\personal\layout-editor\temp\tbb21_20080605oss_src.tgz
D:\IT\personal\layout-editor\temp

And the full output I get:

Traceback (most recent call last):
  File "D:\IT\personal\layout-editor\third_party.py", line 257, in
<module>
    tbb.Process()
  File "D:\IT\personal\layout-editor\third_party.py", line 247, in
Process
    install_operation.Process( self, 'tbb' )
  File "D:\IT\personal\layout-editor\third_party.py", line 160, in
Process
    build_operation.Process( self, name )
  File "D:\IT\personal\layout-editor\third_party.py", line 138, in
Process
    self.Extract()
  File "D:\IT\personal\layout-editor\third_party.py", line 125, in
Extract
    extract( file, self._outdir )
  File "D:\IT\personal\layout-editor\third_party.py", line 43, in
untar
    tarball = tarfile.open( file )
  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



More information about the Python-list mailing list