Request for feedback on my first Python program
Cliff Wells
clifford.wells at attbi.com
Fri May 30 05:18:29 EDT 2003
On Thu, 2003-05-29 at 23:14, 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.
Welcome. There is no better place.
> 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.
Not too bad for a first shot, but for something this simple, I don't
think I'd bother having a function to parse the data:
import sys
import os
# Error status codes
returnCodes = {
'Bad Invocation': 1,
}
# ----------------------------------------------------------------------
def usage(programName):
print "Usage: " + os.path.basename(programName) + " [file]"
sys.exit(returnCodes['Bad Invocation'])
# ----------------------------------------------------------------------
if __name__ == '__main__':
if len(sys.argv) != 2:
usage(sys.argv[0])
for line in open(sys.argv[1], 'r'):
line = line.strip()
if line[0] == '#':
continue
print line, "is a",
if os.path.isdir(line):
print "directory"
elif os.path.isfile(line):
print "file"
else:
print "non-directory and non-file"
Regards,
Cliff
More information about the Python-list
mailing list