How do I search and replace text in a file using Python 3?
Here is my code:
import os import sys import fileinput print (\"Text to search for:\") te
Your problem stems from reading from and writing to the same file. Rather than opening fileToSearch for writing, open an actual temporary file and then after you're done and have closed tempFile, use os.rename to move the new file over fileToSearch.
fileToSearch
tempFile
os.rename