In this example, we show how to use IPython to perform common system actions such as downloading a Zip file, extracting it in a new folder, etc. Specifically, we download some social data about anonymous volunteer Facebook users.
The data is freely available on Stanford's SNAP project.
We begin by importing native modules used to download and extract compressed files.
In [1]:
import urllib2, zipfile
In [2]:
url = 'http://ipython.rossant.net/'
In [3]:
filename = 'facebook.zip'
We download the file in memory with urllib2.urlopen.
In [4]:
downloaded = urllib2.urlopen(url + filename)
Now, we create a new folder named data and we save the Zip file in it.
In [5]:
folder = 'data'
In [6]:
mkdir $folder
In [7]:
cd $folder
In [8]:
with open(filename, 'wb') as f:
f.write(downloaded.read())
We use the zipfile module to extract the Zip file in the data folder.
In [9]:
with zipfile.ZipFile(filename) as zip:
zip.extractall('.')
Common system file commands such as ls just work in IPython, even on Windows systems!
In [10]:
ls
In [11]:
cd facebook
In [12]:
ls
We save the data/facebook/ directory as an alias for later. We will be able to enter this directory just with cd fbdata.
In [13]:
%bookmark fbdata