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 shortest words in the given File.
- Python Program to Count Number of Digits in a Text File
- How to run Python scripts
- Python Interview Questions on File Manipulation
Program to Find the Shortest Words in a Text File in Python
Below is the full approach for finding the shortest 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 minimum length of all words using the min(), 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 minimum 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 minimum length of all words using the min(), len() functions # by passing the above words list key= len as the arguments. # Store it in another variable min_len = len(min(wrds_lst, key=len)) # Get all the words which are having the minimum length # using the list comprehension and store it in another variable rslt = [word for word in wrds_lst if len(word) == min_len] # Print the shortest word in a given file. print("The shortest word in a given file:") print(*rslt)
Output:
The shortest word in a given file: is
File Content:
hello this is btechgeeks good morning btechgeeks
Google Colab Images:
Files and Code: