Connor Tepfer
Connor Tepfer

Reputation: 181

How do I extract a tar file using python 2.4?

I'm trying to extract a .tar file completely using python 2.4.2 and because of this not all of the aspects of the tarfile module are usable. I've looked through the python documentary and I have not found it to be helpful as I continue to make syntax errors. The following are commands I have tried(to no success):

tarfile.Tarfile.getnames(tarfile.tar)
tarfile.Tarfile.extract(tarfile.tar)

Is there a simple way to extract my tar completely? If so what is the formatting used? Also, I'd like to note that tarfile.TarFile.extractall() is not available in my python version.

Upvotes: 18

Views: 53497

Answers (2)

Shital Shah
Shital Shah

Reputation: 68908

Here's the more general code from torchvision library:

import os
import hashlib
import gzip
import tarfile
import zipfile

def _is_tarxz(filename):
    return filename.endswith(".tar.xz")


def _is_tar(filename):
    return filename.endswith(".tar")


def _is_targz(filename):
    return filename.endswith(".tar.gz")


def _is_tgz(filename):
    return filename.endswith(".tgz")


def _is_gzip(filename):
    return filename.endswith(".gz") and not filename.endswith(".tar.gz")


def _is_zip(filename):
    return filename.endswith(".zip")


def extract_archive(from_path, to_path=None, remove_finished=False):
    if to_path is None:
        to_path = os.path.dirname(from_path)

    if _is_tar(from_path):
        with tarfile.open(from_path, 'r') as tar:
            tar.extractall(path=to_path)
    elif _is_targz(from_path) or _is_tgz(from_path):
        with tarfile.open(from_path, 'r:gz') as tar:
            tar.extractall(path=to_path)
    elif _is_tarxz(from_path):
        with tarfile.open(from_path, 'r:xz') as tar:
            tar.extractall(path=to_path)
    elif _is_gzip(from_path):
        to_path = os.path.join(to_path, os.path.splitext(os.path.basename(from_path))[0])
        with open(to_path, "wb") as out_f, gzip.GzipFile(from_path) as zip_f:
            out_f.write(zip_f.read())
    elif _is_zip(from_path):
        with zipfile.ZipFile(from_path, 'r') as z:
            z.extractall(to_path)
    else:
        raise ValueError("Extraction of {} not supported".format(from_path))

    if remove_finished:
        os.remove(from_path)

Upvotes: 5

André Laszlo
André Laszlo

Reputation: 15537

This example is from the tarfile docs.

import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()

First, a TarFile object is created using tarfile.open(), then all files are extracted using extractall() and finally the object is closed.

If you want to extract to a different directory, use extractall's path parameter:

tar.extractall(path='/home/connor/')

Edit: I see now that you are using an old Python version which doesn't have the TarFile.extractall() method. The documentation for older versions of tarfile confirms this. You can instead do something like this:

for member in tar.getmembers():
    print "Extracting %s" % member.name
    tar.extract(member, path='/home/connor/')

If your tar file has directories in it, this probably fails (I haven't tested it). For a more complete solution, see the Python 2.7 implementation of extractall

Edit 2: For a simple solution using your old Python version, call the tar command using subprocess.call

import subprocess
tarfile = '/path/to/myfile.tar'
path = '/home/connor'
retcode = subprocess.call(['tar', '-xvf', tarfile, '-C', path])
if retcode == 0:
    print "Extracted successfully"
else:
    raise IOError('tar exited with code %d' % retcode)

Upvotes: 47

Related Questions