- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
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 Line Number of a Given Word in text file using Python?
In this article, we will show you how to get a line number in which the given word is present from a text file using python.
Assume we have taken a text file with the name TextFile.txt consisting of some random text. We will return the line numbers in which the given word is present from a text file
TextFile.txt
Good Morning TutorialsPoint This is TutorialsPoint sample File Consisting of Specific source codes in Python,Seaborn,Scala Summary and Explanation Welcome TutorialsPoint 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.
Create a variable (which holds the line number) and initialize its value to 1.
Enter the word as static/dynamic input and store it in a variable.
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:
Traverse in each line of the text file using the for loop.
Use the split() function(splits a string into a list. We can define the separator; the default separator is any whitespace) to split each line of a text file into a list of words and store it in a variable.
Using the if conditional statement and “in” keyword, check whether the given word is present in the above words list.
The in keyword works in two ways −
The in keyword is used to determine whether a value exists in a sequence (list, range, string etc).
Print the line number, if the given word is found in that corresponding line.
Increase the value of the line number by 1.
Close the input file with the close() function(used to close an opened file).
It is also used to iterate through a sequence in a for loop
Example
The following program to delete a given line from a text file and print the result file content after deleting that line −
# input text file inputFile = "ExampleTextFile.txt" # storing the current line number lineNumber = 1 # Enter the word givenWord = "TutorialsPoint" print('The word {', givenWord, '} is present in the following lines:') # Opening the given file in read-only mode. with open(inputFile, 'r') as fileData: # Traverse in each line of the file for textline in fileData: # Splitting the line into list of words wordsList = textline.split() # Checking if the given word is present in the above words list if givenWord in wordsList: # Print the line number, if the given word is found print(lineNumber) # Increase the value of linenumber by 1 lineNumber += 1 # Closing the input file fileData.close()
Output
On executing, the above program will generate the following output −
The word { TutorialsPoint } is present in the following lines: 1 2 6
We read a text file containing some random text in this program. We created a variable to store the current line number and initialized it to 1, the starting line number. We proceeded through the text file line by line, breaking each line down into a list of words and checking to see if the given word was in the list. If it is present, it prints the current line Number. For every line, the value of the line number is increased by one.
We learned how to read the file, traverse the file line by line, and get all the words in that line from this article. Once we get them, we may reverse the words, change the case, check the vowels, retrieve word length, and so on. We also learned how to calculate the line number and how to search for a word in a file, which is primarily utilized in some common daily applications such as looking for the name in results, searching for a keyword in some code, and so on.
- Related Articles
- How to Find the Most Repeated Word in a Text File using Python?
- How to write a single line in text file using Python?
- How to read complete text file line by line using Python?
- How to find and replace the word in a text file using PowerShell?
- How to read an entire line from a text file using Python?
- How to Count Word Occurrences in a Text File using Shell Script?
- How to read a number of characters from a text file using Python?
- How to Delete Specific Line from a Text File in Python?
- How to Find the Shortest Words in a Text File using Python?
- How to Find the Longest Words in a Text File using Python?
- Split a File at Given Line Number
- How to find the number of digits in a given number using Python?
- How to count the number of words in a text file using Java?
- How to count the number of lines in a text file using Java?
- How to Copy Odd Lines of Text File to Another File using Python
