[Courses] [python] Lesson 4: Modules and command-line arguments
Erin McLaughlin
emclaughlin1215 at gmail.com
Sat Jul 16 01:24:17 UTC 2011
> ========================= Homework ============================
>
> 1. With the little example I gave earlier, the one that used
> num = int(sys.argv[1]):
> if you run it and don't give an argument, you'll get an error.
> Why? Can you think of a way to check whether the user forgot to
> supply an argument, and print an error message if so?
Because you're trying to get something that does not exist.
import sys
args = sys.argv
if len(args) != 2 :
print "Error!"
else :
print "Good!"
> 2. Write a program that takes a filename and prints the number of
> lines in the file. (You can check its results with wc -l filename.)
import sys
args = sys.argv
if len(args) != 2 :
print "Usage: hw4 FILE"
else :
f = open(args[1])
count = 0
for line in f :
count += 1
print "The file", args[1], "has", count, "lines."
> 3. How would you extend this so that you can count lines in multiple
> files, not just one? So you could say
> $ mywordcounter file1 file2 file3
import sys
args = sys.argv
if len(args) < 2 :
print "Usage: hw4 FILE1 [FILE2]..."
else :
for fn in args[1:] :
f = open(fn)
count = 0
for line in f :
count += 1
print "The file", fn, "has", count, "lines."
> 4. Here's a harder problem, an exercise in debugging (which is a big
> part of programming, sadly):
>
> a. Write a program that counts words in a file (or multiple files,
> if you prefer). Use the same split() and len() you used in
> lesson 2.
import sys
args = sys.argv
if len(args) != 2 :
print "Usage: hw4 FILE"
else :
f = open(args[1])
count = 0
for line in f :
words = line.split(" ")
count += len(words)
print "The file", args[1], "has", count, "words."
> b. Compare the number of words from your program to what wc -w gives.
> (If you're on a platform that doesn't have wc, run it on a small
> file and count by hand.) Are the answers the same?
One off on the particular file I'm using.
> c. Here's the debugging part: why aren't they the same?
> (You don't have to fix it: just figure out the problem.)
>
> Hint: if you're splitting each line into a list, try printing
> the list to see what's in it. In python, if you have a list
> called words, you can just say print words -- you don't have to
> do anything fancy like you would in some languages
Since I'm counting the words in a python file, it's counting each colon
that comes after a for statement as a word, since there's a space before
it. Except this apparently is incorrect? Because there are more than
one colons in the file I'm using. So the problem is blank lines/lines
with just spaces in them.
> d. OPTIONAL, harder: fix the problems and make your word count
> program give the same answer as wc -w.
import sys
args = sys.argv
if len(args) != 2 :
print "Usage: hw4 FILE"
else :
f = open(args[1])
count = 0
for line in f :
line = line.strip()
if len(line) > 0 :
words = line.split(" ")
count += len(words)
for w in words :
print w
else :
continue
print "The file", args[1], "has", count, "words."
Victory!!
More information about the Courses
mailing list