Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #TestLongestWord.py
- #Python 3.4
- #Chris Clarke
- #03.11.2015
- def longestWord(inF, outF):
- outStr = ""
- try:
- with open(inF) as f:
- for line in f:
- words = line.split()
- maxLength = 0
- for word in words:
- if len (word)>maxLength:
- maxLength = len(word)
- if maxLength>0:
- outStr += str(maxLength) + "\n"
- except IOError:
- print("Error: can't open file")
- try:
- fw = open(outF, "w")
- fw.write(outStr)
- except IOError:
- print ("Error: can't write to file")
- else:
- fw.close()
- def main():
- longestWord("trouble.txt", "troubleLongest.txt")
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement