In this tutorial, we’ll try to understand how to search and replace all occurrences of a text in a file using Python in two different ways.
- Search and replace text in a different file – create a new file with matching text replaced.
- Search and replace text in the same file – modify the original file itself.
Method 1 – Search and replace text in a different file
In this method, We first open the input file in read mode and open a new file in write mode which can be used as an output file. Then read each line from the input file and replace the text with the new text and write the replaced line into the output file. Then we close both files.
Example
Now let us try to understand the above method using some worked out examples.
Example 1
input_file = open("input.txt", "rt") output_file = open("output.txt", "wt") for line in input_file: output_file.write(line.replace('Cars', 'EV')) input_file.close() output_file.close()
Steps Followed:
- Opened the input file in the read mode
- Opened a new output file in the write mode
- Loop through each line in the input file
- Replace the text in each line
- Close both the files
Initial file:
Modified file:
Method 2 – Search and replace text in the same file
In this method, We first open the input file in read mode and read all the text into a variable. Then replace all the occurrences with the new text and close the file. Then open the same file in write mode and write the replaced text into the opened file. Then close the file
Example
Now let us try to understand the above method using some worked out examples.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Example 1
input_file = open("data.txt", "rt") text = input_file.read() text = text.replace('Cars', 'Ev') input_file.close() output_file = open("data.txt", "wt") output_file.write(text) output_file.close()
Steps Followed:
- Opened the input file in the read mode
- Read all the content into a variable
- Replace the text and close the file
- Open the same file in write mode
- Write the text into the output file
- Close the file
Initial File:
Modified file:
You might also be interested in –
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.