how to rename a file and keep the original
-2
I would like to keep unaltered the template.txt file after I insert some text into it and save the altered text file with a new name. Currently, my code overwrites the 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')
python rename
share | improve this question
asked Nov 22 '18 at 1...