Request for feedback on my first Python program
Andreas Jung
andreas at andreas-jung.com
Fri May 30 02:40:37 EDT 2003
It's nice to see that one of the world most famous C++ guys uses Python
too ...welcome :-)
Andreas
--On Donnerstag, 29. Mai 2003 23:14 Uhr -0700 Scott Meyers
<Usenet at aristeia.com> 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
>
> # Error status codes
> 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 i in range(0,len(lines)):
> 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...
> 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
> --
> http://mail.python.org/mailman/listinfo/python-list
More information about the Python-list
mailing list