How to Search and Replace a Line in a File in Python?

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file?

Example: Let’s consider the following example.

Text in the file before replacing a line:

There was an idea to bring together a group of remarkable people to see if we could become something more. This line explains the idea behind the Avengers Initiative and what the Avengers were meant to be.

Text in the file after replacing a line:

There was an idea to bring together a group of remarkable people to see if we could become something more. This line explains the idea behind the Finxters Initiative and what the Finxters were meant to be.

Solutions:

In our solutions, the file taken into consideration is named demo.txt, and the path has been mentioned as the path in my local system. While using the code in your program, please replace them accordingly.

Method 1: Loop Through Each Line and Use the string.replace() Method

The usual/conventional way of solving our problem is to loop through each line in the text file and find the text/string that has to be replaced and then replace it with the new string using the replace() method. This has been demonstrated in the following snippet given below (please follow the comments for a better grip on the code):  

#open file in read mode
file = open("demo.txt", "r")
replaced_content = ""

#looping through the file
for line in file:
    
    #stripping line break
    line = line.strip()

    #replacing the texts
    new_line = line.replace("Avengers", "Finxters")

    #concatenate the new string and add an end-line break
    replaced_content = replaced_content + new_line + "\n"

    
#close the file
file.close()

#Open file in write mode
write_file = open("demo.txt", "w")

#overwriting the old file contents with the new/replaced content
write_file.write(replaced_content)

#close the file
write_file.close()

Method 2: Write the Contents to Be Replaced to a New File and Replace the Old File

Before diving into the code, it is important that we take note of the following methods in python:

  • mkstemp() → returns a tuple with a file descriptor and a path.
  • open() → To read or write a file, you have to open it using Python’s built-in open() function. It is used to create a file object, which is then utilized to call other support methods associated with it.
  • copymode() → method in Python used to copy the permission bits from the given source path to a given destination path. The shutil.copymode() method does not affect the file content or owner and group information.
  • move() → method in Python, which allows you to move files from one location to another. 
  • remove() → method in Python, which allows you to remove or delete a file path.

Now that we know why each of the above methods is used, let us have a look at how the code works (please follow the comments for a better grip on the code):  

#importing necessary functions and modules
from tempfile import mkstemp
from shutil import move, copymode
from os import fdopen, remove

#store the path of the file in a variable
path="C:\\Users\Shubham-PC\Desktop\python\method2\demo.txt"

#define the replace function
def replace(file_path, Avengers, Finxters):
    
   #Create temp file
   fd, abs_path = mkstemp()
   with fdopen(fd,'w') as new_file:
       with open(file_path,'r') as old_file:
           for line in old_file:
               new_file.write(line.replace(Avengers,Finxters))
               
   #Copy the file permissions from the old file to the new file
   copymode(file_path, abs_path)
   
   #Remove original file
   remove(file_path)
   
   #Move new file
   move(abs_path, file_path)

   
#calling the replace() method
replace(path,'Avengers','Finxters')

Method 3: Using Fileinput.fileinput() and In-Place Operator

fileinput() → method in Python which allows you to accept a file as an input and then update or append the data in the file.

The following code demonstrates the usage of fileinput() method for replacing text in a file.

import fileinput
import sys

def replace(file, searchExp, replaceExp):
   for line in fileinput.input(file, inplace=1):
       line = line.replace(searchExp, replaceExp)
       sys.stdout.write(line)
       
old_txt = "Avengers"
new_txt = "Finxters"
file = "demo.txt"
replace(file, old_txt, new_txt)

Method 4: Use the Regex Module

Another way of solving our problem is to make use of Python’s regex module. The code given below uses the following regex module functions:

  • re.compile() → used to compile a regular expression pattern and convert it into a regular expression object which can then be used for matching.
  • re.escape() → used to escape special characters in a pattern.

Also, note that the sub() function is used to replace a pattern (Avengers in this example) with a string (Finxters in this example) or result of a function.

#importing the regex module
import re

#defining the replace method
def replace(filePath, text, subs, flags=0):
    
   #open the file
   with open(file_path, "r+") as file:

       #read the file contents
       file_contents = file.read()
       text_pattern = re.compile(re.escape(text), flags)
       file_contents = text_pattern.sub(subs, file_contents)
       file.seek(0)
       file.truncate()
       file.write(file_contents)

       
file_path="demo.txt"
text="Avengers"
subs="Finxters"

#calling the replace method
replace(file_path, text, subs)

Conclusion

Therefore, to search and replace a string in Python you can either load the entire file and then replace the contents in the same file as we did in our conventional method (Method 1) or you may opt to use a more efficient way using context managers as explained in Method 2 or you may even opt to select the regex module and play around with numerous choices. 

I hope you found this article useful. Stay tuned for future updates!