Computational Methods in the Civic Sphere at Stanford University
Extracting and Reading Shakespeare
A walkthrough of modules, file system operations, and Shakespeare.
The following guides are meant to be read while doing the exercises in:
0004-shakefiles
How to create a directory idempotently with makedirs()
A quick tutorial on using the os.makedirs() function to create directories
Downloading and saving the Shakespeare zip with requests
How to download and save a file to disk. And how to properly resolve a file's full path, including its directory.
Unzipping the Shakespeare zipfile with the shutil module
Unpacking archives exactly where we want with the shutil.unpack_archive() function
Reading and printing the first 5 lines of Shakespeare's Hamlet
How to read a text file, line by line.
Count the lines of Shakespeare's Hamlet
To count all the lines in a file, we have to read every line in the file.
Print the final 5 lines of Romeo and Juliet
In order to read just the final 5 lines about these star-crossed lovers, we have to first read the entire tragedy.
Print the final 5 lines of each of Shakespeare's tragedies
Using a for-loop inside another for-loop to experience a whole lot of tragedy.
Counting the non-blank-lines in Shakespeare's tragedies
A simple text-processing and analysis exercise, using everything we've learned so far about file paths, for-loops, and conditional branching.