Request for feedback on my first Python program

Michele Simionato mis6 at pitt.edu
Fri May 30 12:53:31 EDT 2003


Andrew Walkingshaw <andrew-usenet at lexical.org.uk> wrote in message news:<slrnbdeevn.694.andrew-usenet at athena.jcn.srcf.net>...
> 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

I am a Physicists too, and not a "real" programmer, but it seems to me
you could improve your script by using "line.strip()" instead of
"line.lstrip().rstrip()".

HTH,

                        Michele




More information about the Python-list mailing list