Search and replace text in a file in Python

Hello techies, In this tutorial we are going to learn about searching particular text from a file and replacing it with new text.

Search and replace text in a file in Python

We will try to solve this problem in 2 ways:

  • Without using with.
  • Using with.

Without using with :

Approach : 

  • We will store the path of the file in a variable.
  • Then define a function that will have logic of solution.
  • we will retrieve the data from file.
  • Then we will search and replace the text.

Implementation

 

# Define function
def search_and_replace(path,old_text,new_text):
    
    # Accessing file 
    file_to_read = open(path,'r')
    text = file_to_read.read() # retrieving data
    print("Text before replacing " + text)
    file_to_read = open(path,'w') 
    text = text.replace(old_text,new_text) # replacing the text
    file_to_read.write(text) # writing data
    
    file_to_read.close()
    return text

path = "File_to_change.txt" # here we have to paste the path to the file
new_text = 'Tutorial'
old_text= 'tutorial'

# calling the fuction
result = search_and_replace(path,old_text,new_text)

# printing the result
print("Text after replacing"+result) 


Output

Text before replacing : In this tutorial we are going to learn about search and replace text in a file in Python.

Text after replacing :  In this Tutorial we are going to learn about search and replace text in a file in Python.

Explanation:

  • We are accessing the file and storing it in a variable using open() in read mode(‘r’).
  • we are storing the content in text variable.
  • After that we close the file using close() method.
  • Again accessing the file and storing it in a variable using open() but in write mode(‘w’).
  • By using replace method we are replacing the text .
  • Then using write method we are the new text into the file
  • Lastly we are closing the file using close method.

Using with :

Approach : 

  • We will store the path of the file in a variable.
  • Then define a function where will have logic of solution.
  • We will access the file using with statement.
  • Then we search and replace the text.

Implementation

 

def search_and_replace(path,old_text,new_text):
    text=""
    # Accessing the file using with statement
    with open(path,'r') as file_to_read:
        text = file_to_read.read() # reading the file
        print("Text before replacing: "+ text)

    with open(path,'w') as file_to_read:
        text = text.replace(old_text,new_text) # replacing the text
        file_to_read.write(text) # writing data
    
    return text
path = 'File_to_change.txt' # here we have to paste the path to the file
new_text = 'Tutorial'
old_text= 'tutorial'

# calling the function
result = search_and_replace(path,old_text,new_text)

# printing result
print("Text After replacing: "+data)

Output

Text before replacing : In this tutorial we are going to learn about search and replace text in a file in Python.

Text after replacing :  In this Tutorial we are going to learn about search and replace text in a file in Python.

Explanation

  • We are accessing the file and storing it in a variable using with statement and open() function in read mode(‘r’).
  • we are storing the content in text variable.
  • Again accessing the file and storing it in a variable using with statement and open() function but in write mode(‘w’).
  • By using replace method we are replacing the text .
  • Then using write method we are the new text into the file

So using with statement we can eliminate the closing step.
Because with statement will look about it for us.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top