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')









share|improve this question


















  • 3





    Write to a new file (ie: new_file.txt) directly instead?

    – Jon Clements
    Nov 22 '18 at 17:59








  • 2





    change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

    – Filip Młynarski
    Nov 22 '18 at 18:01













  • thanks @FilipMłynarski

    – alexv
    Nov 22 '18 at 18:39











  • The trivial name for renaming a file while keeping the original is "copying".

    – Klaus D.
    Nov 22 '18 at 19:14
















-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')









share|improve this question


















  • 3





    Write to a new file (ie: new_file.txt) directly instead?

    – Jon Clements
    Nov 22 '18 at 17:59








  • 2





    change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

    – Filip Młynarski
    Nov 22 '18 at 18:01













  • thanks @FilipMłynarski

    – alexv
    Nov 22 '18 at 18:39











  • The trivial name for renaming a file while keeping the original is "copying".

    – Klaus D.
    Nov 22 '18 at 19:14














-2












-2








-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')









share|improve this question














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













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 17:58









alexvalexv

465




465








  • 3





    Write to a new file (ie: new_file.txt) directly instead?

    – Jon Clements
    Nov 22 '18 at 17:59








  • 2





    change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

    – Filip Młynarski
    Nov 22 '18 at 18:01













  • thanks @FilipMłynarski

    – alexv
    Nov 22 '18 at 18:39











  • The trivial name for renaming a file while keeping the original is "copying".

    – Klaus D.
    Nov 22 '18 at 19:14














  • 3





    Write to a new file (ie: new_file.txt) directly instead?

    – Jon Clements
    Nov 22 '18 at 17:59








  • 2





    change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

    – Filip Młynarski
    Nov 22 '18 at 18:01













  • thanks @FilipMłynarski

    – alexv
    Nov 22 '18 at 18:39











  • The trivial name for renaming a file while keeping the original is "copying".

    – Klaus D.
    Nov 22 '18 at 19:14








3




3





Write to a new file (ie: new_file.txt) directly instead?

– Jon Clements
Nov 22 '18 at 17:59







Write to a new file (ie: new_file.txt) directly instead?

– Jon Clements
Nov 22 '18 at 17:59






2




2





change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

– Filip Młynarski
Nov 22 '18 at 18:01







change f = open("template.txt", "w") to sth like f = open("template_new.txt", "w") and remove os.rename since your new content will be saved in template_new.txt so you dont need to rename any file.

– Filip Młynarski
Nov 22 '18 at 18:01















thanks @FilipMłynarski

– alexv
Nov 22 '18 at 18:39





thanks @FilipMłynarski

– alexv
Nov 22 '18 at 18:39













The trivial name for renaming a file while keeping the original is "copying".

– Klaus D.
Nov 22 '18 at 19:14





The trivial name for renaming a file while keeping the original is "copying".

– Klaus D.
Nov 22 '18 at 19:14












1 Answer
1






active

oldest

votes


















1














As people have mentioned, you're going to want to copy the contents of template.txt into a new file and then edit this new file. This allows you to keep the original file unmodified and you don't have to worry about renaming files at the end. Another tip: the with open(file) as f syntax keeps you from having to remember to close files when you're editing them and is the recommended way of working with files in python



with open("template.txt") as f:
lines = f.readlines()
with open("new_file.txt", "w+") as n:
lines.insert(2, "This is a custom inserted line n")
n.writelines(lines)





share|improve this answer
























  • The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

    – alexv
    Nov 22 '18 at 23:11













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436170%2fhow-to-rename-a-file-and-keep-the-original%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














As people have mentioned, you're going to want to copy the contents of template.txt into a new file and then edit this new file. This allows you to keep the original file unmodified and you don't have to worry about renaming files at the end. Another tip: the with open(file) as f syntax keeps you from having to remember to close files when you're editing them and is the recommended way of working with files in python



with open("template.txt") as f:
lines = f.readlines()
with open("new_file.txt", "w+") as n:
lines.insert(2, "This is a custom inserted line n")
n.writelines(lines)





share|improve this answer
























  • The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

    – alexv
    Nov 22 '18 at 23:11


















1














As people have mentioned, you're going to want to copy the contents of template.txt into a new file and then edit this new file. This allows you to keep the original file unmodified and you don't have to worry about renaming files at the end. Another tip: the with open(file) as f syntax keeps you from having to remember to close files when you're editing them and is the recommended way of working with files in python



with open("template.txt") as f:
lines = f.readlines()
with open("new_file.txt", "w+") as n:
lines.insert(2, "This is a custom inserted line n")
n.writelines(lines)





share|improve this answer
























  • The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

    – alexv
    Nov 22 '18 at 23:11
















1












1








1







As people have mentioned, you're going to want to copy the contents of template.txt into a new file and then edit this new file. This allows you to keep the original file unmodified and you don't have to worry about renaming files at the end. Another tip: the with open(file) as f syntax keeps you from having to remember to close files when you're editing them and is the recommended way of working with files in python



with open("template.txt") as f:
lines = f.readlines()
with open("new_file.txt", "w+") as n:
lines.insert(2, "This is a custom inserted line n")
n.writelines(lines)





share|improve this answer













As people have mentioned, you're going to want to copy the contents of template.txt into a new file and then edit this new file. This allows you to keep the original file unmodified and you don't have to worry about renaming files at the end. Another tip: the with open(file) as f syntax keeps you from having to remember to close files when you're editing them and is the recommended way of working with files in python



with open("template.txt") as f:
lines = f.readlines()
with open("new_file.txt", "w+") as n:
lines.insert(2, "This is a custom inserted line n")
n.writelines(lines)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 22 '18 at 18:36









NMerklNMerkl

414




414













  • The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

    – alexv
    Nov 22 '18 at 23:11





















  • The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

    – alexv
    Nov 22 '18 at 23:11



















The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

– alexv
Nov 22 '18 at 23:11







The string that I insert is in the format: string_1="hello Bob", string_2="hello Mary", etc. How can create a for loop and concatenate the iterator to pick up every time a different string? E.g., for i in range(1,5,1): blah blah lines.insert(2,string_$in)

– alexv
Nov 22 '18 at 23:11






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436170%2fhow-to-rename-a-file-and-keep-the-original%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()