[Courses] [python] Lesson 4: Modules and command-line arguments

Leslie leslie.brothers at verizon.net
Sun Jul 10 16:55:45 UTC 2011


On Fri, 2011-07-08 at 13:56 -0700, Akkana Peck wrote:

> ========================= 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?
There is no list sys.argv, thus there is nothing to access.

>  Can you think of a way to check whether the user forgot to
>    supply an argument, and print an error message if so?

import sys
if len(sys.argv) == 1 :
	print "You didn't enter any arguments!"
else : 
	num = int(sys.argv[1]) 
	for i in range(0, num) :
		print i


> 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
file = open(sys.argv[1])
x = 0
for line in file :
	x +=1
file.close()
print "There are",x,"lines in",sys.argv[1]

> 
> 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
p = 0
for n in range (1, len(sys.argv)) :
	file = open (sys.argv[n])
 	x = 0
	for line in file :
		x += 1
	file.close()
	print "There are",x,"lines in", sys.argv[n]+"."
	p = x + p
print "There are",p,"lines altogether."

> 
> 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.
> 
>    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?
> 
>    c. Here's the debugging part: why aren't they the same?
>       (You don't have to fix it: just figure out the problem.)
> 
As we saw, I inadvertently used line.split(), which gives the same
results as wc -w.  Here is what I went on to learn about line.split('
').

I found that when using line.split(' '), a blank line in my file
produced a list consisting of one item, '\n' (len = 1). By contrast,
when using line.split(), a blank line produced the empty list [] (len =
0).

Thus I got extra word counts in the case of files containing empty
lines, compared with wc -c, when using line.split(' ').

Since '\n' appears routinely at the end of lines, it makes me think that
'\n' is counted by line.split (' ') only when preceded by white space. I
tried a quick google to check this guess, but nothing jumped out and I
ran out of time.  (Note to self: more research is needed.)

Using strip() did not make a difference for the files I was testing,
although it might for other files, or possibly I was not doing it
correctly.  So I stayed on the track of the '\n' problem.

I came up with a fix (see below) that seems contrived, revealing that I
still don't understand the core functionality of line.split(' ') vs.
line.split().

This was my first program that accidentally worked:
# count words using line.split()
import sys
file = open(sys.argv[1])
y =0
for line in file :
	x = len(line.split())
	y = y + x
print "Result using line.split():",y
file.close()

Here is one using line.split(' ') and a fix:
#count words using line.split(' ')
import sys
file = open(sys.argv[1])
y = 0
for line in file :
	r = len(line.split(' '))
	b = line.split(' ')
	if b[0] == '\n' :
		r = r-1
	y = y + r
print "Result using line.split(' '):",y
file.close()

Even though this answer is not a great success, it was fun to stumble
over the problem in the way that I did and get Akkana's helpful
response. Now I look forward to the REAL answer!

-Leslie



More information about the Courses mailing list