Don't like ads? PRO users don't see any ads ;-)
Guest

8.5

By: dvdjaco on Feb 18th, 2012  |  syntax: Python  |  size: 0.44 KB  |  hits: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # #!/usr/bin/python
  2. #
  3. # By dvdjaco
  4. # Exercise 8.5
  5. #
  6.  
  7. f = raw_input('Enter a file name: ')
  8.  
  9. try:
  10.         fhand = open(f)
  11.         count = 0
  12.         for line in fhand:
  13.                 words = line.split()
  14.                 # print 'Debug:', words
  15.                 if len(words) == 0 or words[0] != 'From' : continue
  16.                 print words[1]
  17.                 count = count + 1
  18.         print "There were", count, "lines with 'From' as first word"
  19.  
  20. except IOError, err :
  21.                 print "Error opening file", f
  22.                 print "Error was:", err