How to Print Lines Containing Given String in File using Python?


In this article, we will show you how to print all the lines that contain a given specific string in a given text file using python.

Assume we have taken a text file with the name ExampleTextFile.txt consisting of some random text. We will return the lines from a text file containing a given particular string from a text file.

ExampleTextFile.txt

Good morning to TutorialsPoint
This is TutorialsPoint sample File
Consisting of Specific
source codes in Python,Seaborn,Scala
Summary and Explanation
Welcome to TutorialsPoint
Learn with a joy
Good morning to TutorialsPoint

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.

  • Enter the string 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

  • Using the if conditional statement and “in” keyword, check whether the given string is present in the above line data.

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, if the given string is found in that corresponding line.

  • Close the input file with the close() function (used to close an opened file).

Example

The following program checks line by line if the given string is found in a line from a text file and prints the line if the string is found −

# input text file inputFile = "ExampleTextFile.txt" # Enter the string givenString = "to TutorialsPoint" print('The following lines contain the string {', givenString, '}:') # Opening the given file in read-only mode with open(inputFile, 'r') as filedata: # Traverse in each line of the file for line in filedata: # Checking whether the given string is found in the line data if givenString in line: # Print the line, if the given string is found in the current line print(line) # Closing the input file filedata.close()

Output

On executing, the above program will generate the following output −

The following lines contain the string { to TutorialsPoint }:
Good morning to TutorialsPoint
Welcome to TutorialsPoint
Good morning to TutorialsPoint

We read a text file containing some random text in this program. We read the text file line by line, then checking to see if the given string appeared in that line data. If it is present, then we printed the line's current value (Total Line Value).

Conclusion

We learned how to read the file, traverse the file line by line, and get all the line data this article. Once we get them, we may reverse the line, change the case, find number of words in that line, check the vowels, retrieve line characters, and so on. We also learned how to search for a string in a file and print the relevant line, which is mostly used in typical daily applications like looking for an id and printing all of the person's information.

Updated on: 18-Aug-2022

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements