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
You can do the replacement like this
f1 = open('file1.txt', 'r') f2 = open('file2.txt', 'w') for line in f1: f2.write(line.replace('old_text', 'new_text')) f1.close() f2.close()