Request for feedback on my first Python program
Martin Franklin
MFranklin1 at gatwick.westerngeco.slb.com
Fri May 30 03:46:08 EDT 2003
Scott Meyers wrote:
> I'm a C++ programmer who's writing his very first Python program. This
> means the program is going to be gross, and I apologize for that in
> advance. I don't really have anybody I can show it to for feedback, so I'm
> hoping I can get some comments here. If there is a better place for me to
> seek guidance, please let me know.
>
> The program is supposed to read a file containing directory and file names,
> one per line. (The file can also contain comment and blank lines, which
> should be ignored.) For each file or directory name, the program should
> print out whether it's a directory, a file, or neither. That's it.
>
> Here's the code; you may want to hold your nose:
>
> import sys
> import os
> import string
as of Python 2.0 the string module is not needed (string's have methods...)
>
> # Error status codes
> BadInvocation = 1
# conventionally all CAPS for constants
BADINVOCATION = 1
>
> def usage(programName):
> print "Usage: " + os.path.basename(programName) + " [file]"
print "Usage:", os.path.basename(programName), "[file]"
# Normally bad practice to add strings together comma inserts a space so...
> sys.exit(BadInvocation)
>
> # Take a list of strings, return an equivalent list where the following
> # strings have been removed:
> # - those with no non-whitespace characters
> # - those whose first non-whitespace character is a "#"
> def getMeaningfulLines(lines):
> nonCommentLines = []
> for i in range(0,len(lines)):
lines is a python list object so you can iterate over it's items directly
def getMeaningfulLines(lines):
nonCommentLines = []
for line in lines:
if line.startswith("#") or not line:
continue
else:
# call line.strip() to get rid of unwanted \n\r at EOL
# in fact this is what was causing your program to fail....
nonCommentLines.append(line.strip())
return nonCommentLines
> try:
> firstToken = string.split(lines[i])[0]
> if firstToken[0] != "#":
> nonCommentLines.append(lines[i])
> except IndexError:
> continue
> return nonCommentLines
>
>
> # if this language had a main(), it'd be here...
# it does..
if __name__=="__main__":
# this part only executed when called as first arg to python
# not when imported
> if len(sys.argv) != 2: usage(sys.argv[0])
>
> lines = getMeaningfulLines(open(sys.argv[1]).readlines())
> for i in lines:
> print i + " is a ",
> if os.path.isdir(i): print "directory"
> elif os.path.isfile(i): print "file"
> else: print "non-directory and non-file"
>
> In addition to the numerious stylistic gaffes I'm sure I've made, the
> program also doesn't work correctly. Given this input file,
>
> d:\
> d:\temp\foo.py
>
> I get this output:
>
> d:\
> is a non-directory and non-file
> d:\temp\foo.py
> is a non-directory and non-file
>
> Aside from being ugly (how do I get rid of the newline that follows each
> directory or file name?), the problem is that the first entry IS a
> directory and the second one IS a file. So clearly I'm doing something
> wrong. Any idea what it is?
>
> Thanks very much in advance.
>
> Scott
So here it is (tested on my linux machine only....)
import sys
import os
# Error status codes
# conventionally all CAPS for constants
BADINVOCATION = 1
def usage(programName):
print "Usage:", os.path.basename(programName), "[file]"
sys.exit(BADINVOCATION)
# Take a list of strings, return an equivalent list where the following
# strings have been removed:
# - those with no non-whitespace characters
# - those whose first non-whitespace character is a "#"
def getMeaningfulLines(lines):
nonCommentLines = []
for line in lines:
if line.startswith("#") or not line:
continue
else:
nonCommentLines.append(line.strip())
return nonCommentLines
# if this language had a main(), it'd be here...
# it does.. we just spell it like so:-
if __name__=="__main__":
# this part only executed when called as first arg to python
# not when this module is imported
if len(sys.argv) != 2:
usage(sys.argv[0])
lines = getMeaningfulLines(open(sys.argv[1]).readlines())
for i in lines:
print i, "is a",
if os.path.isdir(i):
print "directory"
elif os.path.isfile(i):
print "file"
else:
print "non-directory and non-file"
HTH
Martin
More information about the Python-list
mailing list