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:

  1. Open the input file in read mode and handle it in text mode.
  2. Open the output file in write mode and handle it in text mode.
  3. For each line read from the input file, replace the string and write to the output file.
  4. 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

  1. Open data.txt in read text rt mode and assign it to the reference fin.
  2. Open out.txt in write text wt mode and assign it to the reference fout.
  3. For each line in fin, the string pyton is replaced with python, and the modified line is written to fout.
  4. Close both the input and output files using fin.close() and fout.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

  1. Open data.txt in read text mode rt.
  2. Read the entire contents of the file into the variable data using fin.read().
  3. Use data.replace() to replace all occurrences of pyton with python in the entire text.
  4. Close the input file with fin.close().
  5. Open the same file in write mode wt and overwrite it with the modified content using fin.write(data).
  6. 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

  1. Open the file textfile.txt in read text mode rt.
  2. Open the output file outputfile.txt in write text mode wt.
  3. For each line in fin, replace all instances of example with sample and write the modified line to fout.
  4. 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.


Python Libraries