Replace word in file python – Python Program to find and replace a word in a Text File

Files in Python:

Replace word in file python: Python file handling is a way of saving program output to a file or reading data from a file. File handling is a crucial concept in the programming world. File management is used in almost every form of project. Assume you are constructing an inventory management system. You have data in the inventory management system related to sales and purchases, thus you must save that data somewhere. Using Python’s file management, you can save that data to a file. You must be given data in the form of a comma-separated file or a Microsoft Excel file if you wish to conduct data analysis. Using file handling, you can read data from a file and write output back into it.

Given a file, the task is to find and replace a word in the given file in Python.

Program to find and replace a word in a Text File in Python

Below is the full approach for finding and replacing a word in the given file in Python.

Approach:

  • Give some random word that you want to replace as user input using the input() function and store it in a variable.
  • Give some random word with which you want to replace as user input using the input() function and store it in another variable.
  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Read the above lines of the file using the read() function(get the content) and store it in a variable.
  • Pass the word, and the word with which you want to replace as arguments to the replace() function and store it in the same variable.
  • Open the file in write mode. In this case, we’re writing the contents to the file.
  • Write the above file text content to the given file using the write() function.
  • The Exit of Program

Below is the implementation:

# Give some random word that you want to replace as user input using the input() function
# and store it in a variable.
gvn_wrd = input("Type some random word you want to replace:")
# Give some random word with which you want to replace as user input using the input() function
# and store it in another variable.
rplce_word = input("Type some random word with which you want to replace:")

# Make a single variable to store the path of the file. This is a constant value. 
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Read the above lines of the file using the read() function(get the content) and store it in a variable
    filetext = givenfilecontent.read()
    # Pass the word, and the word with which you want to replace as arguments to the replace()
    # function and store it in the same variable.
    filetext = filetext.replace(gvn_wrd,rplce_word)
   
# Open the file in write mode. In this case, we're writing the contents to the file.  
with open(givenFilename,"w") as givenfilecontent:
    # write the above file text content to the given file using the write() function
    givenfilecontent.write(filetext)

Output:

Type some random word you want to replace:hello 
Type some random word with which you want to replace:goodmorning

goodmorning 123 this is 567 btechgeeks

File Content before replacing:

hello 123 this is 567 btechgeeks

File Content after replacing:

goodmorning 123 this is 567 btechgeeks

Explanation:

  • The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
  • Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
  • The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
  • When we open the file, we use the character ‘r’ to signify read-mode.
  • We traverse each line of the file using the For loop and replace the given word with the word to be replaced using replace() function.

Google Colab Images:

Files and Code: