Skip to content

Instantly share code, notes, and snippets.

@ptomulik
Created October 12, 2015 00:23
Show Gist options
  • Save ptomulik/2d988ae760b992da62f8 to your computer and use it in GitHub Desktop.
Save ptomulik/2d988ae760b992da62f8 to your computer and use it in GitHub Desktop.
Download and untar file
#! /usr/bin/env python
def download_and_untar(url, **kw):
import os
import tarfile
from io import BytesIO
try: from urllib.request import urlopen
except ImportError: from urllib import urlopen
# Options
try: strip_components = kw['strip_components']
except KeyError: strip_components = 0
# Download the tar file
tar = tarfile.open(fileobj = BytesIO(urlopen(url).read()))
members = [m for m in tar.getmembers() if len(m.name.split(os.sep)) > strip_components]
if strip_components > 0:
for m in members:
m.name = os.path.join(*(m.name.split(os.sep)[strip_components:]))
tar.extractall(members = members)
tar.close()
url = "https://github.com/CxxTest/cxxtest/archive/master.tar.gz"
download_and_untar(url, strip_components = 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment