Я бы хотел оставить неизменным файл template.txt после того, как вставил в него какой-нибудь текст и сохранил измененный текстовый файл под новым именем.В настоящее время мой код перезаписывает template.txt.
f = open("template.txt", "r")
contents = f.readlines()
f.close()
#insert the new text at line = 2
contents.insert(2, "This is a custom inserted line \n")
#open the file again and write the contents
f = open("template.txt", "w")
contents = "".join(contents)
f.write(contents)
f.close()
os.rename('template.txt', 'new_file.txt')