I have written this method in order to replace text in line. It prints the correct text as I want it to be replaced but it is not updating those changes in the file. I am quite new to Python, Could you please help me where I am making a mistake?
def regFind(regx, sub_text, file_name):
c_file = io.StringIO(file_name)
for line in c_file:
match = regx.search(line)
if match:
replaced_line = re.sub(regx, sub_text, line)
line = replaced_line
#print(line)
yield line
regx = re.compile(r'^MYTEXT')
sub_text = 'NewText'
a_file = open('file.txt').read()
for line in regFind(regex, sub_text, a_file):
print(line)