Reputation: 3620
I have a .tar.gz file which I want to unpack (when I unpack with 7-Zip manually, I am getting a .tar file inside). I am able to unpack this .tar file easily then with Python tarfile
module then.
When I right-click the .tar.gz file in Windows Explorer, I can see under Type of file: 7-Zip.gz (.gz). I have tried using gzip module (gzip.open
), however I am getting a an exception 'Not a gzipped file'
. So there should be some other way to go.
I have searched the Internet and seen that people use 7-Zip manually or some batch commands, however I cannot find a way to do this in Python. I am on Python 2.7.
Upvotes: 0
Views: 3858
Reputation: 1667
The tarfile library is able to read gzipped tar files. You should look at the examples here:
http://docs.python.org/2/library/tarfile.html#examples
The first example might accomplish what you want. It extracts the content of the archive to the current working directory:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
Upvotes: 2
Reputation: 2682
import os
import tarfile
import zipfile
def extract_file(path, to_directory='.'):
if path.endswith('.zip'):
opener, mode = zipfile.ZipFile, 'r'
elif path.endswith('.tar.gz') or path.endswith('.tgz'):
opener, mode = tarfile.open, 'r:gz'
elif path.endswith('.tar.bz2') or path.endswith('.tbz'):
opener, mode = tarfile.open, 'r:bz2'
else:
raise ValueError, "Could not extract `%s` as no appropriate extractor is found" % path
cwd = os.getcwd()
os.chdir(to_directory)
try:
file = opener(path, mode)
try: file.extractall()
finally: file.close()
finally:
os.chdir(cwd)
Found this here: http://code.activestate.com/recipes/576714-extract-a-compressed-file/
Upvotes: 1
Reputation: 1134
This is the example from the python-docs and should work:
import gzip
f = gzip.open('file.txt.gz', 'rb')
file_content = f.read()
f.close()
Upvotes: 0