import re f = open("filename", "w") data = f.read() data = re.sub("initialthing", "replaceitwith", data) f.write(data) f.close()