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.
import urllib2, zipfile
url = 'http://ipython.rossant.net/'
filename = 'facebook.zip'
We download the file in memory with urllib2.urlopen
.
downloaded = urllib2.urlopen(url + filename)
Now, we create a new folder named data
and we save the Zip file in it.
folder = 'data'
mkdir $folder
cd $folder
chapter2\data
with open(filename, 'wb') as f:
f.write(downloaded.read())
We use the zipfile
module to extract the Zip file in the data
folder.
with zipfile.ZipFile(filename) as zip:
zip.extractall('.')
Common system file commands such as ls
just work in IPython, even on Windows systems!
ls
facebook.zip
cd facebook
chapter2\data\facebook
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
.
%bookmark fbdata