Python Workout — Finding the longest word from a file
The following code snippet finds the longest word(s) from a file. It opens the file, reads all the contents and then outputs the longest words based on their length. The snippet comes from the internet, and as it works fine I’ve just reproduced it here. What I like about it is that it outputs multiple longest words, instead of only one. I think this approach is better than just updating a single variable when it encounters a long word of equal length.
This the code:
This code works fine for a file with one word per line. With other formatted documents, I had mixed results. I suspect this has something to do with the length of such documents. Reading from files using Python keeps being hit or miss for some reason.