Problem: A file contains some text where in few words, you need to write a program to replace a list of those words by ###### by updating the same file.
Code:
words=['brainless', 'mindless', 'dull']
with open('sampleDonkey.txt') as f:
    content=f.read()
    print(content)
print()
for word in words:
    newcontent=content.replace(word, '######')
    with open('sampleDonkey.txt','w') as f:
            f.write(newcontent)
I have used the same code for replacng a single word in the same file and its working fine, the only difference in the code is list, here, it only replaces the last word i.e. dull. 
What mistake am I maing in this code?
The file that needs to be updated is enclosed.																			Instructor
Yogesh Chawla Replied on 19/11/2023
We didn't put the code in right order.
Please check this:
words=['brainless', 'mindless', 'dull']
# creating a variable and storing the text that we want to add
replace_text = "#####"
# Opening our text file in read only
# mode using the open() function
with open(r'sampleDonkey.txt', 'r') as file:
    # Reading the content of the file
    # using the read() function and storing
    # them in a new variable
    data = file.read()
    # Searching and replacing the text
    # using the replace() function
    for word in words:
        data = data.replace(word, replace_text)
# Opening our text file in write only
# mode to write the replaced content
with open(r'sampleDonkey.txt', 'w') as file:
    # Writing the replaced data in our
    # text file
    file.write(data)
# Printing Text replaced
print("Text replaced")						
											
Couldn't find what this "r" in the below syntax is for anywhere? Why do we use this?
with open(r'sampleDonkey.txt', 'w') as file:						
											Instructor
Yogesh Chawla Replied on 29/11/2023