Chapter 2, example 1

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


chapter2\data

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


facebook.zip

In [11]:
cd facebook


chapter2\data\facebook

In [12]:
ls


0.circles
0.edges
107.circles
107.edges
1684.circles
1684.edges
1912.circles
1912.edges
3437.circles
3437.edges
348.circles
348.edges
3980.circles
3980.edges
414.circles
414.edges
686.circles
686.edges
698.circles
698.edges

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