Python Program to Find the Longest Words in a Text File

Files in Python:

One of the most important subjects for programmers and automation testers is Python file handling (also known as File I/O). It is necessary to work with files in order to write to or read data from them.

Also, if you didn’t know, I/O operations are the most expensive processes where a programme can go wrong. As a result, you must use extreme caution while implementing file processing for reporting or any other reason. Optimizing a single file action can help in the creation of a high-performing application or a reliable automated software testing solution.

Consider the following scenario: you’re planning to construct a large Python project with a large number of workflows. Then it’s unavoidable that you don’t make a log file. You’ll also be handling the log file’s read and write activities. Debugging huge applications with log files is a terrific way to go. It’s usually better to consider a scalable design from the start, as you won’t be sorry later if you didn’t.

Given a file, the task is to find the longest words in the given File.

Program to Find the Longest Words in a Text File in Python

Below is the full approach for finding the longest words in the given File.

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Get all the words in a file using the read(), split() functions.
  • Store it in a variable.
  • Calculate the maximum length of all words using the max(), len() functions by passing the above words list key= len as the arguments.
  • Store it in another variable.
  • Get all the words which are having the maximum length using the list comprehension and store it in another variable.
  • Print the above result.
  • The Exit of Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Get all the words in a file using the read(), split() functions
    # Store it in a variable
    wrds_lst = givenfilecontent.read().split()
# Calculate the maximum length of all words using the max(), len() functions
# by passing the above words list key= len as the arguments.
# Store it in another variable
max_len = len(max(wrds_lst, key=len))
# Get all the words which are having the maximum length
# using the list comprehension and store it in another variable
rslt = [word for word in wrds_lst if len(word) == max_len]
# Print the above result
print(*rslt)

Output:

btechgeeks welcomeall

File Content:

hello
thisis 
btechgeeks
welcomeall

Explanation:

Here btechgeeks and welcomeall are the longest words which are of length 10.

Google Colab Images:

Files and Code: