- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Find the Shortest Words in a Text File using Python?
In this article, we will show you how to print all the shortest words from a given text file using python. The shortest words are the words which are having the same length as the shortest word (minimum length) in a text file.
Assume we have taken a text file with the name TextFile.txt consisting of some random text. We will return all the shortest words from a given text file.
Good Morning Tutorials Point This is the Tutorials Point sample File Consisting of Specific abbreviated source codes in Python Seaborn Scala Imagination Summary and Explanation Welcome user Learn with joy
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 shortest 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 min()(returns the lowest-valued item in an iterable)functions from the above words list.
len(min(words List, key=len))
Using the list comprehension, get all the words with the shortest length and save them in another variable. Here we are traversing each word of the file and checking whether the length of that word is equal to the length of the shortest word using the for loop in a list comprehension.
The key=len specifies that we must obtain the word depending on its length, and we will obtain the minimum length word using the min() function and the length of the minimum length word using the len() function.
Print all the shortest words from a given text file.
Close the input file with the close() function(used to close an opened file).
When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.
The following program checks the length of the shortest word and prints all the words which are having the same length as that of the shortest 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 from a file wordsList = filedata.read().split() # finding the length of the shortest word in the above words list shortestWordLength = len(min(wordsList, key=len)) # Storing all the words having the minimum length(shortest word length) result = [textword for textword in wordsList if len(textword) == shortestWordLength] # Print the shortest words from a text file print("The following are the shortest words from a text file:") print(result) # Closing the input file filedata.close()
On executing, the above program will generate the following output −
The following are the shortest words from a text file: ['is', 'of', 'in']
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 minimum 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.
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 minimum length, we learned how to scan the entire file content for the minimum length words.
- Related Articles
- How to Find the Longest Words in a Text File using Python?
- How to count the number of words in a text file using Java?
- How to Find the Most Repeated Word in a Text File using Python?
- How to Find Line Number of a Given Word in text file using Python?
- How to search and replace text in a file using Python?
- How to write a single line in text file using Python?
- How to find and replace within a text file using Python?\n\n\n
- How to find and replace the word in a text file using PowerShell?
- How to write multiple lines in text file using Python?
- How to find a file using Python?
- How to read a text file in Python?
- How to Copy Odd Lines of Text File to Another File using Python
- How to work with a text file in Python?
- How to read an entire line from a text file using Python?
- How to read a number of characters from a text file using Python?