Python Program to Find and Replace a Word in a Text File

Python Program to Find and Replace a Word in a Text File

In this article, we will be solving an interesting question of Data File Handling based on text file. According to the question we have to find and replace a word in a text file.

If you like the article content, then share it in your coding groups so that more people can take advantage of the content. So, without wasting any time let's solve this question.

Question - Write a Python Program to Find and Replace a Word in a Text File

Python Program to Find and Replace a Word in a Text File

Logic and Explanation

Question - Write a Python Program to Find and Replace a Word in a Text File

Python Program to Find and Replace a Word in a Text File - Suppose we are given a text file "content.txt" and we have to find and replace a word in a text file.


First of all we will take input from user which word he want to replace and with which word he want to replace.


c = input("Enter the word you want to replace")
r = input("Enter with which word you want to replace")

Now we will open the text file in read mode and then we will read the whole file content in one go and will store it in a string. Then, we will use the built in replace method of string to replace the word according to the user input.

with open("content.txt","r") as f:
    text = f.read()
    text = text.replace(c,r)


Now we will again open the text file but not in write mode and then we will write the text string with replaced words in this. Opening in write mode will overwrite the text file and we will finally get a file with replaced words as per user input.

with open("content.txt","w") as f:
    f.write(text)


That's done. Now, run the program and  enter the required inputs and you will see that the required are replaced. I hope this program, this question is  clear to you. In case of any doubt feel free to ask in the comments below. 

| Check More - Python Programs for Practice |

Here's the complete source code for the Python Program to Find and Replace a Word in a Text File.

c = input("Enter the word you want to replace")
r = input("Enter with which word you want to replace")

#Reading the text file
with open("content.txt","r") as f:
    text = f.read()

    #Replacing the required word
    text = text.replace(c,r)
    
# Writing Edited text in File    
with open("content.txt","w") as f:
    f.write(text)

So, I hope you liked the article and you would have found the content of the article useful and helpful for you. Share this article in your coding communities so that more people can take advantage of the content. Also, In case of any doubt feel free to ask in the comments below.

For More Such Python Programs - Click Here

Enjoy Coding!!!


Also Read :-

Extract Phone Number Details Using Python





For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment

For any doubts feel free to ask in comments below.
Stay Connected to Us for more such Courses and Projects.

Post a Comment (0)

Previous Post Next Post