Week 5: Scripting

Before getting started, you should download the following two files and open them in a text editor (so you get syntax highlighting): http://stanford.edu/~jainr/basics.py has some python basics and http://stanford.edu/~jainr/tester.py has some code examples.  You should also check out Python's Popen and os modules -- os.system works for a lot of stuff, but it's a pretty blunt tool, and you can do a lot more with Popen and the os commands.

0. Pre-Lab Questions

What is the difference between subprocess.Popen and os.system?

How would you access command-line arguments from a python script?

1.  Write a Basic Script

Download the following file: 

shakespeare_sonnets.txt 

Write a script that finds how many times the word "love" appears in the file. We'll count it even if it's a part of a word, say "lovely", and also if it's capitalized as in "Love". Verify with us!

If your script isn't already set up this way, make it so that your script can find the number of occurrences of ANY word. HINT: This involves pulling from the argumtents to the script.

How many times does the word "thee" appear?

How about the word "to"?

How about the word "eternal"?

2. Write a scripting script

Now write a script that you can run ONCE, which determines the number of occurrences of the words "love", "thee", "to", and "eternal" from shakespeare_sonnets.txt all at once. This is a script that will run a script. It's almost like "meta" scripting. You should only need to run this script once!