Advertisement
brilliant_moves

TestLongestWord.py

Nov 3rd, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.77 KB | None | 0 0
  1. #!/usr/bin/python
  2. #TestLongestWord.py
  3. #Python 3.4
  4. #Chris Clarke
  5. #03.11.2015
  6.  
  7. def longestWord(inF, outF):
  8.     outStr = ""
  9.     try:
  10.         with open(inF) as f:
  11.             for line in f:
  12.                 words = line.split()
  13.                 maxLength = 0
  14.                 for word in words:
  15.                     if len (word)>maxLength:
  16.                         maxLength = len(word)
  17.                 if maxLength>0:
  18.                     outStr += str(maxLength) + "\n"
  19.     except IOError:
  20.         print("Error: can't open file")
  21.  
  22.     try:
  23.         fw = open(outF, "w")
  24.         fw.write(outStr)
  25.     except IOError:
  26.         print ("Error: can't write to file")
  27.     else:
  28.         fw.close()
  29.  
  30. def main():
  31.     longestWord("trouble.txt", "troubleLongest.txt")
  32.  
  33. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement