How to Find the Longest Words in a Text File using Python?


In this article, we will show you how to print all the longest words from a given text file using python. The longest words are the words which are having the same length as the longest word (maximum length) in a text file.

Assume we have taken a text file with the name ExampleTextFile.txt consisting of some random text. We will return all the longest words from a given text file.

ExampleTextFile.txt

Good Morning Tutorials Point
This is Tutorials Point sample File
Consisting of Specific
abbreviated
source codes in Python Seaborn Scala
Imagination
Summary and Explanation
Welcome user
Learn with a joy

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the path of the text file.

  • Use the open() function (opens a file and returns a file object as a result) to open the text file in read-only mode by passing the file name, and mode as arguments to it  (Here “r” represents read-only mode).

with open(inputFile, 'r') as filedata:
  • Create a variable to read the text file data using the read() function (reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file) and split it into a list of words of a given text file using the split() function (splits a string into a list. We can define the separator; the default separator is any whitespace).

  • Find the length of the longest word using the len() (The number of items in an object is returned by the len() method. It returns the number of characters in a string when the object is a string) and max() (returns the highest-valued item, or the highest-valued item in an iterable) functions from the above words list.

len(max(words List, key=len))
  • The key=len specifies that we must obtain the word depending on its length, and we will obtain the maximum length word using the max() function and the length of the maximum length word using the len() function.

  • Using the list comprehension, get all the words with the longest length and save them in another variable. Here we are traversing in each word of the file and checking whether the length of that word is equal to the length of the longest word using the for loop in a list comprehension.

list comprehension:
  • When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

  • Print all the longest words from a given text file.

  • Close the input file with the close() function (used to close an opened file).

Example

The following program checks the length of the longest word and prints all the words which are having the same length as that of the longest word from a given text file −

# input text file inputFile = "ExampleTextFile.txt" # Opening the given file in read-only mode. with open(inputFile, 'r') as filedata: # Getting the list of words of a file wordsList = filedata.read().split() # finding the length of the longest word in the above words list longestWordLength = len(max(wordsList, key=len)) # Storing all the words having the maximum length(longest word length) # Here, we are checking all the words whose length is equal to that of the longest word result = [textword for textword in wordsList if len(textword) == longestWordLength] # Print the longest words from a text file print("The following are the longest words from a text file:") print(result) # Closing the input file filedata.close()

Output

On executing, the above program will generate the following output −

The following are the longest words from a text file:
['abbreviated', 'Imagination', 'Explanation']

We read some random text from a text file in this program. We read over the entire file and break it down into words. We determined the length of the maximum length word after we obtained the words. Then we went through the file word by word, checking whether the length of the corresponding word was equal to the length of the minimal length word. If it is true, we will print those words and close the open file.

Conclusion

So, from this article, we learned how to read the entire file content at once, which is useful for searching any word in the entire document rather than searching line by line. We also learned how to split the file content into words using the split() function and determining the length of the shortest word. After determining the maximum length, we learned how to scan the entire file content for the maximum length words.

Updated on: 18-Aug-2022

12K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements