How to unpack 7-Zip.gz (.gz) with Python?

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


The tarfile library is able to read gzipped tar files. You should look at the examples here:

The first example might accomplish what you want. It extracts the content of the archive to the current working directory:

import tarfile
tar ="sample.tar.gz")

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 =, 'r:gz'
    elif path.endswith('.tar.bz2') or path.endswith('.tbz'):
        opener, mode =, 'r:bz2'
        raise ValueError, "Could not extract `%s` as no appropriate extractor is found"     % path

    cwd = os.getcwd()

        file = opener(path, mode)
        try: file.extractall()
        finally: file.close()

Found this here:

This is the example from the python-docs and should work:

import gzip
f ='file.txt.gz', 'rb')
file_content =

Need Your Help

iphone development: using touch down and touch up inside simultaneosly

iphone objective-c ios cocoa-touch user-interaction

In my app I use a button and I assign two methods to them one is working when you touch down( the button image is changed), the other is working when you touch up inside (another view is opened). so

MDS config manager

sql sql-server tsql sql-server-2012 master-data-services

Every time I open the Master Data Services Configuration Manager, all options that I previously configured are blank. The service is working fine but aparently the manager doesnt keep the configs s...