Python Program to Replace Text in a File

This article is created to cover some programs in Python that replaces text in file. Here are the list of programs available in this article:

Things to do before Program

Since the program given below operates on file. Therefore a text file say fresherearth.txt must be available in the current directory (the folder where the Python program's source code is saved). Therefore create a file with following content:

My name is fresherearth.txt
You are learning Python.

and save this as fresherearth.txt. Here is the snapshot of the file created in the current directory:

python replace text in file

And here is the snapshot of the opened file named fresherearth.txt:

python file replace text program

Now let's move on and create a program that replaces some text with new text as provided by user at run-time, from the file.

Replace Text with New Text in File

The question is, write a Python program that replaces any particular text available in a file with a new text. Here the name of file, old text to search, and new text to replace with, must be entered by user at run-time. The answer to this question is the program given below:

print("Enter the Name of File: ")
filename = input()
filehandle = open(filename, "r")
content = filehandle.read()
filehandle.close()

print("Enter text to search: ")
text = input()
print("Enter text to replace with: ")
replace = input()

if text in content:
    content = content.replace(text, replace)
    filehandle = open(filename, "w")
    filehandle.write(content)
    filehandle.close()
    print("\nThe text is replaced!")
else:
    print("Not Found!")

The snapshot given below shows the initial output produced by this Python program:

replace text in file python

Now supply all the inputs as asked by program and required by you. After providing inputs say fresherearth.txt as file name, learning as text to search, practicing as text to replace, here is the sample run with exactly same inputs:

python program replace text in file

Now if you see the same file as created above, the content of it gets changed. That is, in place of learning, you will see the text practicing will be available. Here is the new snapshot of the opened file named fresherearth.txt:

replace text from file python file

Replace Text from File and See New Content

Now I've changed the content of file, fresherearth.txt with content as shown in the following snapshot:

python replace old text with new in file

Now let's create the modified version of previous program, that replaces text from given file and prints old and new content of the file:

print("Enter File's Name: ", end="")
filename = input()

filehandle = open(filename, "r")
content = filehandle.read()
filehandle.close()

print("Enter text to search for: ", end="")
text = input()
print("Enter text to replace with: ", end="")
replace = input()

oldcontent = content

if text in content:
    content = content.replace(text, replace)
    filehandle = open(filename, "w")
    filehandle.write(content)
    filehandle.close()
    print("\nThe text is replaced successfully!")
    print("\n-------Old Content-------")
    print(oldcontent)
    print("\n------New Content-------")
    print(content)
else:
    print("\nNot Found!")

Here is its sample run with user input, fresherearth.txt as file's name, Python as text to search for, JavaScript as text to replace with:

python replace text in file

And here is the new snapshot of the same file after executing the above program:

replace text with new text python file

Python Online Test


« Previous Program Next Program »