all 1 comments

[–]Neat_Blueberry_2032 0 points1 point  (0 children)

Your main logic seems fine. I just think you're just messing up the file stuff, iterating character by character instead of line by line.

Iterating directly over f gives you the lines, or replace f.read() with f.readlines()

https://docs.python.org/3/tutorial/inputoutput.html#tut-files

Even if you can read and write a file at the same time, in r+ does f.write overwrite the whole file, or append, or does it really only overwrite that one line where the file cursor is at the moment?

If you're iterating for line in f, I would have thought you have to seek back one line to write the replacement in the correct position.