Search for a Specific Word in a File in Python

In Python, you can search for a specific word in a file using the with open() function to read the file and check for occurrences of the word using the in keyword, find(), or re.search() for regex-based searches. Below are different ways to do this with examples.


Examples

1. Searching for a Word in a File Using a Basic Loop

In this example, we read the file sample.txt line by line using a For loop and checks if the target word "Python" is present in the line using the in keyword.

sample.txt

main.py

</>
Copy
# Define the file name
file_name = "sample.txt"

# Define the word to search
search_word = "Python"

# Open the file and search for the word
with open(file_name, "r") as file:
    for line in file:
        if search_word in line:
            print("Found:", line.strip())

Explanation:

  • file_name: Specifies the file to read.
  • search_word: The word we are looking for in the file.
  • with open(file_name, "r"): Opens the file in read mode.
  • for line in file: This For loop iterates through each line of the file.
  • if search_word in line: This if statement checks if the word exists in the current line.
  • print("Found:", line.strip()): Prints the line containing the word, removing extra spaces using string strip() method.

Output (if the word exists in the file):

2. Finding the Line Number of the Word in a File

This method locates the word and prints the line number where it appears.

In this example, we locate the word "Python" in the sample.txt file, and print the line number where it appears in the file.

sample.txt

main.py

</>
Copy
# Define the file and search word
file_name = "sample.txt"
search_word = "Python"

# Open the file and search for the word with line numbers
with open(file_name, "r") as file:
    for line_number, line in enumerate(file, start=1):
        if search_word in line:
            print(f"Found at line {line_number}: {line.strip()}")

Explanation:

  • enumerate(file, start=1): Iterates over lines, keeping track of line numbers.
  • if search_word in line: Checks if the word is in the current line.
  • print(f"Found at line {line_number}: {line.strip()}"): Displays the line number where the word was found.

Output:

3. Counting the Number of Occurrences of Search String in a File

This method counts how many times the word appears in the file.

In this example, we count the occurrences of the word "Python" in the sample.txt file.

sample.txt

main.py

</>
Copy
# Define file and search word
file_name = "sample.txt"
search_word = "Python"

# Initialize count
count = 0

# Open file and count occurrences
with open(file_name, "r") as file:
    for line in file:
        count += line.lower().count(search_word.lower())

print(f"The word '{search_word}' appears {count} times.")

Explanation:

  • count: Keeps track of occurrences.
  • line.lower().count(search_word.lower()): Ensures case-insensitive search. References: string lower(), string count()

Output:

4. Using Regular Expressions for More Flexible Searching

We use re.search() to search for a word with variations like case differences or partial matches.

In this example, we search for the word "Python" in the sample.txt file using re.search().

sample.txt

main.py

</>
Copy
import re

# Define file and search word
file_name = "sample.txt"
search_word = r"\bPython\b"  # Exact word match using regex

# Open file and search using regex
with open(file_name, "r") as file:
    for line in file:
        if re.search(search_word, line, re.IGNORECASE):
            print("Match found:", line.strip())

Explanation:

  • re.search(): Searches for an exact word match.
  • \b: Ensures the word is matched as a whole.
  • re.IGNORECASE: Makes the search case-insensitive.

Output:

Conclusion

In this tutorial, we covered multiple ways to search for a word in a file in Python:

  1. Basic Loop: Checks if a word exists in a line.
  2. Finding Line Numbers: Identifies the exact location of a word.
  3. Counting Occurrences: Counts how many times a word appears.
  4. Regex Search: Finds flexible matches with case insensitivity.