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
With a single with block, you can search and replace your text:
with open('file.txt','r+') as f: filedata = f.read() filedata = filedata.replace('abc','xyz') f.truncate(0) f.write(filedata)