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 text files using various methods like reading and searching line by line, reading the entire file, and using regular expressions, or using the grep command.

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 memory-efficient and suitable for large text files.

Syntax

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

Here, the for loop iterates through each line of the file and compares each line 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 ?

def search_string_line_by_line(file_path, search_string):
    try:
        with open(file_path, 'r') as file:
            for line in file:
                if search_string in line:
                    return True
            return False
    except FileNotFoundError:
        print(f"File {file_path} not found")
        return False

# Create a sample file for demonstration
with open('example.txt', 'w') as f:
    f.write("Hello World\nPython programming is fun\nLet's learn together")

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.")
String found in the text file.

Method 2: Reading the Entire File and Using Regular Expressions

For complex pattern matching and when you need to search for patterns rather than exact strings, regular expressions provide powerful functionality.

Syntax

match = re.search(pattern, file_contents)

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

Example

In the below example, we use regular expressions to search for patterns in the file ?

import re

def search_string_with_regex(file_path, pattern):
    try:
        with open(file_path, 'r') as file:
            file_contents = file.read()
            match = re.search(pattern, file_contents, re.IGNORECASE)
            return match is not None
    except FileNotFoundError:
        print(f"File {file_path} not found")
        return False

# Create a sample file for demonstration
with open('example.txt', 'w') as f:
    f.write("Hello World\nPython programming is fun\nLet's learn together")

file_path = 'example.txt'
pattern = r'python'  # Case-insensitive search

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

Method 3: Using the grep Command via Subprocess

We can execute shell commands 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. This method works on Unix-like systems.

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 use the subprocess module to execute the grep command ?

import subprocess
import platform

def search_string_with_grep(file_path, search_string):
    # Check if system supports grep
    if platform.system() == 'Windows':
        print("grep command not available on Windows by default")
        return False
    
    try:
        subprocess.check_output(['grep', search_string, file_path])
        return True
    except subprocess.CalledProcessError:
        return False
    except FileNotFoundError:
        print("grep command not found")
        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.")

Comparison

Method Memory Usage Best For Platform Support
Line by line Low Large files, simple searches All platforms
Regular expressions High Pattern matching, complex searches All platforms
grep command Low Fast searches on Unix systems Unix/Linux/macOS

Conclusion

Use line-by-line reading for memory-efficient searches in large files. Choose regular expressions for complex pattern matching. Consider the grep method for fast searches on Unix-like systems.

Updated on: 2026-03-27T08:44:29+05:30

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements