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

Answers


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()

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/


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()

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