Request for feedback on my first Python program

Steven D'Aprano steve at cyber.com.au
Fri May 30 04:59:57 EDT 2003


Apologies if this reply arrives twice, I've had some problems with
posting news articles today.

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.

I don't pretend that this is the best or most Pythonic way of doing
what you ask, since I'm just a beginner myself.

(Tested but not guaranteed totally bug-free.)


Regards, 

Steven.


[start code]

import sys, os, string

# Error status codes
BadInvocation = 1

def usage(programName):
    "Print usage information and die."
    print "Usage: %s [file]" % os.path.basename(programName)
    sys.exit(BadInvocation)

def fileKind(pathname):
    """Given a file path, return the kind of file it is.
Currently only distinguishes between directories, regular files, and
everything else."""
    if os.path.isdir(pathname):
        return "directory"
    elif os.path.isfile(pathname):
        return "file"
    else:
        return "something else"

def listFileKinds(files):
    """Given a list of file paths, list each one and the kind of file
it is."""
    for line in files:
        line = string.strip(line) # delete leading/trailing whitespace
        if (not line) or (string.split(line)[0][0] == "#"):
            # ignore blank lines and comments
            continue
        print "%s is a %s." % (line, fileKind(line))

if __name__ == "__main__":
    if len(sys.argv) != 2:
        usage(sys.argv[0])
    else:
        listFileKinds(open(sys.argv[1]).readlines())

[end code]




More information about the Python-list mailing list