Request for feedback on my first Python program

Andrew Walkingshaw andrew-usenet at lexical.org.uk
Fri May 30 07:09:43 EDT 2003


In article <MPG.194096b51af188bb9896f0 at news.hevanet.com>, Scott Meyers wrote:

> Here's the code; you may want to hold your nose:

I can't talk, I'm a physicist and not a "real" programmer, but here's
how I would approach this problem:


import sys, os

BADINVOCATION = 1

def usage(programName):
    # strings are immutable, so summing strings is generally considered
    # bad style: it's much slower than using string interpolation
    # (though this is a real nitpick in this case)
    print("Usage: %s [file]" % os.path.basename(programName))
    sys.exit(BADINVOCATION)

def isMeaningful(line):
    if line != "" and line[0] != "#":
        return True
    else:
        return False

def main():
    if len(sys.argv) != 2:
        usage(sys.argv[0])
    f = open(sys.argv[1], "r")

    for line in f:
        s = line.lstrip().rstrip()
        if isMeaningful(s):
            if os.path.isdir(s): 
                print "%s is a directory." % s
            elif os.path.isfile(s): 
                print "%s is a file." %s
            else: 
                print "%s is neither a file nor a directory." % s

if __name__ == "__main__":
    main()

- Andrew

-- 
Andrew Walkingshaw | andrew-usenet at lexical.org.uk





More information about the Python-list mailing list