extract tgz files in python

Solutions on MaxInterview for extract tgz files in python by the best coders in the world

showing results for - "extract tgz files in python"
Clover
31 Jan 2018
1import os, sys, tarfile
2
3def extract(tar_url, extract_path='.'):
4    print tar_url
5    tar = tarfile.open(tar_url, 'r')
6    for item in tar:
7        tar.extract(item, extract_path)
8        if item.name.find(".tgz") != -1 or item.name.find(".tar") != -1:
9            extract(item.name, "./" + item.name[:item.name.rfind('/')])
10try:
11
12    extract(sys.argv[1] + '.tgz')
13    print 'Done.'
14except:
15    name = os.path.basename(sys.argv[0])
16    print name[:name.rfind('.')], '<filename>'
17