Is it possible to parse a file line by line, and edit a line in-place while going through the lines?
fileinput module has very ugly API, I find beautiful module for this task - in_place, example for Python 3:
import in_place
with in_place.InPlace('data.txt') as file:
for line in file:
line = line.replace('test', 'testZ')
file.write(line)
main difference from fileinput:
Some useful notes from @rocksNwaves placed in comment