Request for feedback on my first Python program
Anand Pillai
pythonguy at Hotpop.com
Fri May 30 07:50:18 EDT 2003
Hi Scott,
The main problem of your code is in the lines,
lines = getMeaningfulLines((open(sys.argv[1])).readlines())
for i in lines:
print i + " is a ",
...
readlines() return lines from the file till the newline character,
similar to fgets() in C. So what you read in your code is not d:\
or d:\temp\foo.py but 'd:\<many spaces>\n' and
'd:\temp\foo.py<many spaces>\n'.
To fix this you need to modify the code as follows.
for i in lines:
i = string.strip(i) # strip all spaces & newlines
print i + " is a ",
...
Now your code works correctly.
Apart from that the main() function can also be defined in
the program.
Here is a modified program with some changes I thought
made it better.
# scott.py - Scott Meyer's first Effective Python program :-)
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 line in lines:
try:
firstToken = string.split(line)[0]
if firstToken[0] != "#":
nonCommentLines.append(line)
except IndexError:
continue
return nonCommentLines
def whatami(str):
if os.path.isdir(str): return 'directory'
elif os.path.isfile(str): return 'file'
else: return 'non-directory and non-file'
# Here is the main()
def main():
# 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:
i = string.strip(i)
print i, 'is a', whatami(i)
if __name__=="__main__":
main()
Glad to help you pythonically yours,
Anand Pillai
Scott Meyers <Usenet at aristeia.com> wrote in message news:<MPG.194096b51af188bb9896f0 at news.hevanet.com>...
> 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
More information about the Python-list
mailing list