​Why write a python script for this? It just looks like you rewrote

sed -i 's/old/new/g' input.txt