How to unpack .tar.gz files with Python?

01-30-2017 09:17 AM
New Contributor III

I would like do the same thing to multiple Giz and Tarfiles. I already replaced "zipfile" for "tarfile" at the script, but It doesn't works with Python 2.7.1 at ArcMap 10.1 (but will be good which It works with newer ArcMap)

>>> import tarfile,fnmatch,os
... rootPath = r"C:\Teste_Auto_Unzip"
... pattern = '*.tar.gz'
... for root, dirs, files in os.walk(rootPath):
...     for filename in fnmatch.filter(files, pattern):
...         print(os.path.join(root, filename))
...         tarfile.TarFile(os.path.join(root, filename)).extractall(
                os.path.join(root, filename.split(".")[0]))
Runtime error 
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "C:\Python27\ArcGIS10.4\Lib\", line 1581, in __init__
self.firstmember =
File "C:\Python27\ArcGIS10.4\Lib\", line 2356, in next
raise ReadError(str(e))
ReadError: invalid header‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks in advance!

Tags (1)
0 Kudos
2 Replies
MVP Esteemed Contributor

The doc says, and it is a good idea, as the open() method allows you to specify .tar.gz format:

Do not use this class directly, better use instead

12.5. tarfile — Read and write tar archive files — Python 2.7.13 documentation 

0 Kudos
New Contributor III

It works!!!

Thank you

I just replaced: tarfile.TarFile(...


2017-01-30 16:43 GMT-03:00 Curtis Price <>:

GeoNet <>

Re: How to unpack .tar.gz files with Python?

reply from Curtis Price

<> in

Python - View the full discussion


0 Kudos