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).

    It is also used to iterate through a sequence in a for loop

  • 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).

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.

Updated on: 17-Aug-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements