How to search for a string in text files using Python?


Searching for a string in text files is an important task while doing data analysis on text data. In Python, we can search for a string in textfile using various methods like reading and searching line by line, reading the entire file, and using regular expressions,using the grep command, etc.

Method 1:Reading and Searching Line by Line

One straightforward approach is to read the text file line by line and search for the desired string in each line. This method is suitable for smaller text files.

Syntax

for line in file:
            if search_string in line:
                return True
return False

Here, the for loop iterate through each line of the file and compares each word with the search_string. If the search_string is found it returns True else False.

Example

In the below example, we define a function search_string_line_by_line that takes the file_path and search_string as parameters. The function opens the file using the open() function with the mode 'r' to read the file. It iterates through each line in the file and checks if the search_string is present in the line using the in operator. If the string is found, the function returns True, indicating a match. If the loop completes without finding a match, the function returns False.

def search_string_line_by_line(file_path, search_string):
    with open(file_path, 'r') as file:
        for line in file:
            if search_string in line:
                return True
        return False

file_path = 'example.txt'
search_string = 'Python'

if search_string_line_by_line(file_path, search_string):
    print("String found in the text file.")
else:
    print("String not found in the text file.")

Output

String found in the text file.

Method 2:Reading the Entire File and Using Regular Expressions

For larger text files, reading the entire file into memory might be more efficient. We can use Regular expressions for pattern matching, including string searches.

Syntax

match = re.search(search_string, file_contents)

Here, re.search() function takes the search_string and the file contents as parameter and searches for the search_string in the file content.

Example

In the below example, we import the re module to utilize regular expressions. The function search_string_with_regex takes the file_path and search_string as parameters. It opens the file, reads its entire contents using the read() method, and stores it in the file_contents variable. Then, it uses the re.search() function to search for the search_string pattern within file_contents. If a match is found, the function returns True; otherwise, it returns False.

import re

def search_string_with_regex(file_path, search_string):
    with open(file_path, 'r') as file:
        file_contents = file.read()
        match = re.search(search_string, file_contents)
        return match is not None

file_path = 'example.txt'
search_string = r'hello\sworld'

if search_string_with_regex(file_path, search_string):
    print("String found in the text file.")
else:
    print("String not found in the text file.")

Output

String not found in the text file.

Method 3:Using the grep Command via Subprocess

We can execute shell command using the subprocess module of Python. We can utilize this subprocess module to use the powerful grep command−line tool for string searching in text files.

Syntax

subprocess.check_output(['grep', search_string, file_path])

Here, subprocess.check_output() function takes the search string and the file path as input and runs the grep command to find the search/_string in the file content.

Example

In the below example, we import the subprocess module to execute shell commands. The function search_string_with_grep takes the file_path and search_string as parameters. It uses the subprocess.check_output() function to execute the grep command with the specified search_string and file_path as arguments. If the command successfully executes (i.e., the string is found), the function returns True. Otherwise, it returns False.

import subprocess

def search_string_with_grep(file_path, search_string):
    try:
        subprocess.check_output(['grep', search_string, file_path])
        return True
    except subprocess.CalledProcessError:
        return False

file_path = 'example.txt'
search_string = 'Python'

if search_string_with_grep(file_path, search_string):
    print("String found in the text file.")
else:
    print("String not found in the text file.")

Output

String found in the text file.

Conclusion

In this article, we discussed how we can search for a string in a text file using different methods on Python. The first method involved reading and searching line by line, suitable for smaller files. The second method utilized regular expressions to search the entire file contents, enabling more complex pattern matching. The third method leveraged the grep command−line tool via the subprocess module, providing a powerful and flexible approach.

Updated on: 18-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements