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 where you want to replace the highlighted (bolded) text parts.
Text in the file before replacing a line:
Text in the file after replacing a line:
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 most straightforward way to replace a specific line in a file 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
This is 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: Read and Override the Whole File at Once
The following approach is a quick way to replace arbitrary text (e.g., a specific line) in a file:
To replace an arbitrary string (such as a specific line) in a Python file, use the following three steps:
- Open the file in read mode using
open('demo.txt', 'r')and read the whole file contents using
- Create a new string with replaced contents using the
- Open the file in writing mode using
open('demo.txt', 'w')and override it with the new
Here’s a snippet that applies this method to our running code example:
# Read file in read mode 'r' with open('demo.txt', 'r') as file: content = file.read() # Replace string content = content.replace('Avengers', 'Finxters') # Write new content in write mode 'w' with open('demo.txt', 'w') as file: file.write(content)
The problem with this approach is that it may take a long time reading the whole file if it is too large for your computer’s memory. Also, you cannot replace a specific line number as can be done in the for loop in Method 1.
However, it’s a quick way to solve the problem for smaller files.
Method 3: Loop Through Each Line and Use the string.replace() Method
If you want to replace a specific line number, you can use a variant of Method 1.
This is demonstrated in the following snippet given below:
# open file in read mode file = open("demo.txt", "r") replaced_content = "" line_number = 3 i = 0 # looping through the file for line in file: # stripping line break line = line.strip() # replacing the text if the line number is reached if i == line_number: new_line = line.replace("Avengers", "Finxters") else: new_line = line # concatenate the new string and add an end-line break replaced_content = replaced_content + new_line + "\n" # Increase loop counter i = i + 1 # 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 3: 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:\\...\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 4: 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 5: 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)
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!
Where to Go From Here?
Enough theory. Let’s get some practice!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.