Python – Replace String in File with another String
Python – Replace String in File
To replace a string in a file using Python, follow these steps:
- Open the input file in read mode and handle it in text mode.
- Open the output file in write mode and handle it in text mode.
- For each line read from the input file, replace the string and write to the output file.
- Close both the input and output files.
Examples
1. Replace a string in File
Consider that we have a text file with some spelling mistakes. In this example, we replace the string pyton
with python
in the data.txt
file and write the result to out.txt
.
Python Program
#input file
fin = open("data.txt", "rt")
#output file to write the result to
fout = open("out.txt", "wt")
#for each line in the input file
for line in fin:
#read, replace the string and write to output file
fout.write(line.replace('pyton', 'python'))
#close input and output files
fin.close()
fout.close()
Explanation
- Open
data.txt
in read textrt
mode and assign it to the referencefin
. - Open
out.txt
in write textwt
mode and assign it to the referencefout
. - For each line in
fin
, the stringpyton
is replaced withpython
, and the modified line is written tofout
. - Close both the input and output files using
fin.close()
andfout.close()
.
Input File
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Output File
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
The string pyton
in the input file is replaced with python
.
2. Replace string and update the original file
In this example, we replace the string pyton with python in the data.txt file and overwrite the original file with the replaced text.
Python Program
#read input file
fin = open("data.txt", "rt")
#read file contents to string
data = fin.read()
#replace all occurrences of the required string
data = data.replace('pyton', 'python')
#close the input file
fin.close()
#open the input file in write mode
fin = open("data.txt", "wt")
#overwrite the input file with the resulting data
fin.write(data)
#close the file
fin.close()
Explanation
- Open
data.txt
in read text modert
. - Read the entire contents of the file into the variable
data
usingfin.read()
. - Use
data.replace()
to replace all occurrences ofpyton
withpython
in the entire text. - Close the input file with
fin.close()
. - Open the same file in write mode
wt
and overwrite it with the modified content usingfin.write(data)
. - Close the file after writing with
fin.close()
.
Input File
Welcome to www.pytonexamples.org. Here, you will find pyton programs for all general use cases.
Modified Input File
Welcome to www.pythonexamples.org. Here, you will find python programs for all general use cases.
3. Replace String with Multiple Occurrences
In this case, we will replace multiple occurrences of a string in a file, replacing every instance of the string example
with sample
in the textfile.txt
.
Python Program
#input file
fin = open("textfile.txt", "rt")
#output file
fout = open("outputfile.txt", "wt")
#replace all occurrences of 'example' with 'sample'
for line in fin:
fout.write(line.replace('example', 'sample'))
#close files
fin.close()
fout.close()
Explanation
- Open the file
textfile.txt
in read text modert
. - Open the output file
outputfile.txt
in write text modewt
. - For each line in
fin
, replace all instances ofexample
withsample
and write the modified line tofout
. - Close both the input and output files.
Input File
This is an example of a file with several example strings to replace.
Output File
This is an sample of a file with several sample strings to replace.
Summary
In this tutorial of Python Examples, we learned how to replace a string with another string in a file using Python. The examples covered replacing strings in a new output file, updating the original file, and handling multiple occurrences of strings within the file.