tarfile doesn't work with tgz files?

R

Robert Dailey

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. 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. 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.
 
K

Kushal Kumaran

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. 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. 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.

Is the file itself correct? Are you able to extract the files using
any compression program? Do you have a version of tar installed that
you can try extracting the file with? If you don't, a Windows version
of tar will be available with cygwin.
 
R

Robert Dailey

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. 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. 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.

Correction, it is the following line that breaks:

tarfile.open( file )

When I try to *open* the file it fails, not extract. Sorry for the
mixup.
 
G

Gary Herron

Robert said:
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:
-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
 
R

Robert Dailey

That's correct.




Not true.  It's supported.



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
 
R

Robert Dailey

Is the file itself correct?  Are you able to extract the files using
any compression program?  Do you have a version of tar installed that
you can try extracting the file with?  If you don't, a Windows version
of tar will be available with cygwin.

Yes, I can open and extract it using WinRAR
 
R

Robert Dailey

That's correct.




Not true.  It's supported.



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!
 
T

Tim Daneliuk

Robert said:
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. 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. 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.

Several thoughts:

Did you use the r:gz argument in your read statement to specify
the type of compression in use?

Is the file even readable - i.e., Are the permissions proper on the file?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top