Untar a Tar File With Python

Users of Mac, Linux and Unix operating systems have it easy.

Along with the omnipresent zip, they do have ready-to-use tools for most compression formats built right into the operating system.

Windows user lack the luxury. To unzip a tar or tar.gz format in windows, a 3rd party download like winrar is required.

Reluctant to install one more software in my virtual box window, I was looking for a download free solution. This exploration brought me to the standard python module tarfile.

This did the trick.

Here’s a simple small python script to untar a tar or tar.gz file in windows without any download, provided you have python installed!!

# -*- coding: utf-8 -*-
Created on Thu Feb 20 06:59:20 2014

@author: Sukhbinder Singh

Tarfile test

import tarfile,sys

def untar(fname):
    if (fname.endswith("tar.gz")):
        tar = tarfile.open(fname)
        print "Extracted in Current Directory"
        print "Not a tar.gz file: '%s '" % sys.argv[0]

if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage: '%s filename'" % sys.argv[0]

Wow! That’s why python rocks!!

One thought on “Untar a Tar File With Python

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s