How to unpack .tar.gz files with Python?

25086
2
01-30-2017 09:17 AM
BrunoDeus1
New Contributor III

Hi,
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]))
... 
C:\Teste_Auto_Unzip\LC82270692015242LGN00.tar.gz
Runtime error 
Traceback (most recent call last):
File "<string>", line 7, in <module>
File "C:\Python27\ArcGIS10.4\Lib\tarfile.py", line 1581, in __init__
self.firstmember = self.next()
File "C:\Python27\ArcGIS10.4\Lib\tarfile.py", line 2356, in next
raise ReadError(str(e))
ReadError: invalid header‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Thanks in advance!

Tags (1)
0 Kudos
2 Replies
curtvprice
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 tarfile.open() instead

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

0 Kudos
BrunoDeus1
New Contributor III

It works!!!

Thank you

I just replaced: tarfile.TarFile(...

for: tarfile.open(...

2017-01-30 16:43 GMT-03:00 Curtis Price <geonet@esri.com>:

GeoNet <https://community.esri.com/?et=watches.email.thread>

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

reply from Curtis Price

<https://community.esri.com/people/curtvprice?et=watches.email.thread> in

Python - View the full discussion

<https://community.esri.com/message/662700-re-how-to-unpack-targz-files-with-python?commentID=662700&et=watches.email.thread#comment-662700>

0 Kudos