how to rename a file and keep the original
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
add a comment |
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
3
Write to a new file (ie:new_file.txt
) directly instead?
– Jon Clements♦
Nov 22 '18 at 17:59
2
changef = open("template.txt", "w")
to sth likef = open("template_new.txt", "w")
and remove os.rename since your new content will be saved intemplate_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
add a comment |
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
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
python rename
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
changef = open("template.txt", "w")
to sth likef = open("template_new.txt", "w")
and remove os.rename since your new content will be saved intemplate_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
add a comment |
3
Write to a new file (ie:new_file.txt
) directly instead?
– Jon Clements♦
Nov 22 '18 at 17:59
2
changef = open("template.txt", "w")
to sth likef = open("template_new.txt", "w")
and remove os.rename since your new content will be saved intemplate_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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 likef = open("template_new.txt", "w")
and remove os.rename since your new content will be saved intemplate_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