Request for feedback on my first Python program

Ben Finney bignose-hates-spam at and-zip-does-too.com.au
Fri May 30 03:51:13 EDT 2003


On Thu, 29 May 2003 23:14:45 -0700, Scott Meyers wrote:
>   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"

The readlines() method doesn't strip line endings.  Try this:

  lines = getMeaningfulLines(open(sys.argv[1]).readlines())
  for i in lines:
    filename = i.strip()
    print filename + " is a ",
    if os.path.isdir(filename): print "directory"
    elif os.path.isfile(filename): print "file"
    else: print "non-directory and non-file"

The strip() method will strip all leading and trailing whitespace from
the string, where whitespace includes space, tab, CR, LF, FF, VT.

-- 
 \      "God forbid that any book should be banned. The practice is as |
  `\               indefensible as infanticide."  -- Dame Rebecca West |
_o__)                                                                  |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B




More information about the Python-list mailing list