Replace all instances of a string in a file Open the file in read mode using the open() function. Read…