Skip to content

Python program to replace a specific word in a text file with a new word

Python program to replace a specific word in a text file with a new word

In this tutorial we will write a Python program to replace a specific word in a text file with a new word . Before reading further we recommend to read about following articles –

I have a file named file.txt in my current working folder . The contents of the file is shown below –

Python is an interpreted high-level general-purpose programming language

Python program to replace a specific word in a text file with a new word

Steps:

  • First we will open the text file whose contents will be modified .
  • Then we will read the file using read() method .
  • After that we will convert the string returned by read() method to a list using split() method .
  • Then we will traverse the list returned by split() method and change the required word with a given word .
  • After that using join() method we will convert the list to a string .
  • After that we will open a new file in write mode and write the string to the file .
# opening the file
with open("file.txt") as fp:
    data=fp.read().split()

# replacing programming word with coding
for index in range(len(data)):
    if data[index]=="programming":
        data[index]="coding"
# convert the list to string
data=(" ").join(data)

# writing the data in a new file
with open("output.txt","w") as fp:
    fp.write(data)
  • In the above code first we have opened file.txt in read mode . To open the file we have used with keyword so we don’t need to close the file .
  • After data=fp.read().split() line value of data is ['Python', 'is', 'an', 'interpreted', 'high-level', 'general-purpose', 'programming', 'language']
  • Here we have replaced programming word with coding . For this reason we have traversed the data list and searched for programming word . Whenever programming word found it is replaced with coding .
  • So after data[index]="coding" line value of data is ['Python', 'is', 'an', 'interpreted', 'high-level', 'general-purpose', 'coding', 'language']
  • Then we have converted data list to a string using join() method . So after applying join() method value of data is Python is an interpreted high-level general-purpose coding language
  • After that we have opened a new file output.txt in write mode and write the data to the file .

Implementing the above code defining a function

def replace_word_in_file(oldFile,newFile,oldWord,newWord):
    # opening the file
    with open(oldFile) as fp:
        data=fp.read().split()

    # replacing oldWord with newWord
    for index in range(len(data)):
        if data[index]==oldWord:
            data[index]=newWord
    # convert the list to string
    data=(" ").join(data)

    # writing the data in a new file
    with open(newFile,"w") as fp:
        fp.write(data)    

replace_word_in_file("file.txt","output.txt","programming","coding")    

Thank you for reading this Article . If You enjoy it Please Share the article . If you want to say something Please Comment .

Leave a Reply