Python Tutorial

Python Flow Control

Python Functions

Python Data Types

Python Date and Time

Python Files

Python String

Python List

Python Dictionary

Python Variable

Python Input/Output

Python Exceptions

Python Advanced

How to search and replace text in a file in Python

To search and replace text in a file using Python, you can follow these steps:

  1. Open the file in read mode and read its content.
  2. Perform a search and replace operation on the content using the str.replace() method.
  3. Open the file again in write mode and write the modified content back to the file.

Here's an example that demonstrates how to search for the text "old_text" and replace it with "new_text" in a file named "example.txt":

def search_and_replace(filename, old_text, new_text):
    # Read the file content
    with open(filename, 'r') as file:
        content = file.read()

    # Perform search and replace
    modified_content = content.replace(old_text, new_text)

    # Write the modified content back to the file
    with open(filename, 'w') as file:
        file.write(modified_content)

# Example usage
filename = "example.txt"
old_text = "old_text"
new_text = "new_text"

search_and_replace(filename, old_text, new_text)

In this example, the search_and_replace() function takes a filename, the text to search for, and the text to replace it with. It reads the content of the file, performs the search and replace operation, and then writes the modified content back to the file. Note that this approach reads the entire file into memory, so it may not be suitable for very large files.

  1. Replace string in file using Python:

    • Description: Replacing a specific string in a file.
    • Code:
      def replace_string_in_file(file_path, old_string, new_string):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(old_string, new_string)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      replace_string_in_file('example.txt', 'old_text', 'new_text')
      
  2. Python regex search and replace in file:

    • Description: Using regex to search and replace in a file.
    • Code:
      import re
      
      def regex_replace_in_file(file_path, pattern, replacement):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = re.sub(pattern, replacement, file_content)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      regex_replace_in_file('example.txt', r'\bword\b', 'new_word')
      
  3. Edit text file in Python with search and replace:

    • Description: Editing a text file in Python using search and replace.
    • Code:
      def search_replace_in_file(file_path, search_text, replace_text):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(search_text, replace_text)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      search_replace_in_file('example.txt', 'search_this', 'replace_with_this')
      
  4. Search and replace specific text in a file using Python:

    • Description: Searching and replacing specific text in a file.
    • Code:
      def search_replace_in_file(file_path, search_text, replace_text):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(search_text, replace_text)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      search_replace_in_file('example.txt', 'search_this', 'replace_with_this')
      
  5. Python file handling replace text:

    • Description: Handling files in Python to replace text.
    • Code:
      def replace_text_in_file(file_path, old_text, new_text):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(old_text, new_text)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      replace_text_in_file('example.txt', 'old_text', 'new_text')
      
  6. Find and replace text in multiple files Python:

    • Description: Finding and replacing text in multiple files.
    • Code:
      import os
      
      def replace_text_in_files(folder_path, old_text, new_text):
          for filename in os.listdir(folder_path):
              if filename.endswith('.txt'):
                  file_path = os.path.join(folder_path, filename)
      
                  with open(file_path, 'r') as file:
                      file_content = file.read()
      
                  updated_content = file_content.replace(old_text, new_text)
      
                  with open(file_path, 'w') as file:
                      file.write(updated_content)
      
      replace_text_in_files('/path/to/folder', 'old_text', 'new_text')
      
  7. Modify content of a file with Python search and replace:

    • Description: Modifying the content of a file using search and replace.
    • Code:
      def search_replace_in_file(file_path, search_text, replace_text):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(search_text, replace_text)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      search_replace_in_file('example.txt', 'search_this', 'replace_with_this')
      
  8. Replace words in file Python script:

    • Description: Replacing words in a file using a Python script.
    • Code:
      def replace_words_in_file(file_path, old_words, new_words):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          for old_word, new_word in zip(old_words, new_words):
              file_content = file_content.replace(old_word, new_word)
      
          with open(file_path, 'w') as file:
              file.write(file_content)
      
      replace_words_in_file('example.txt', ['old_word1', 'old_word2'], ['new_word1', 'new_word2'])
      
  9. Python file input/output replace text:

    • Description: Replacing text in a file using Python file input/output.
    • Code:
      def replace_text_in_file(file_path, old_text, new_text):
          with open(file_path, 'r') as file:
              file_content = file.read()
      
          updated_content = file_content.replace(old_text, new_text)
      
          with open(file_path, 'w') as file:
              file.write(updated_content)
      
      replace_text_in_file('example.txt', 'old_text', 'new_text')