How to append text to the end of the first line
with open ("a.txt", "a+") as f:
f.write("Hello ")
Currently, this enters into the text file as shown below.
hello
hello
I've also tried
for line in f:
f.write(line.replace("n", ""))
which didn't work.
Any ideas?
python python-3.x
add a comment |
with open ("a.txt", "a+") as f:
f.write("Hello ")
Currently, this enters into the text file as shown below.
hello
hello
I've also tried
for line in f:
f.write(line.replace("n", ""))
which didn't work.
Any ideas?
python python-3.x
How did it not work?
– Mad Physicist
Jan 20 at 6:58
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01
add a comment |
with open ("a.txt", "a+") as f:
f.write("Hello ")
Currently, this enters into the text file as shown below.
hello
hello
I've also tried
for line in f:
f.write(line.replace("n", ""))
which didn't work.
Any ideas?
python python-3.x
with open ("a.txt", "a+") as f:
f.write("Hello ")
Currently, this enters into the text file as shown below.
hello
hello
I've also tried
for line in f:
f.write(line.replace("n", ""))
which didn't work.
Any ideas?
python python-3.x
python python-3.x
asked Jan 20 at 1:55
DanseyDansey
716
716
How did it not work?
– Mad Physicist
Jan 20 at 6:58
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01
add a comment |
How did it not work?
– Mad Physicist
Jan 20 at 6:58
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01
How did it not work?
– Mad Physicist
Jan 20 at 6:58
How did it not work?
– Mad Physicist
Jan 20 at 6:58
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01
add a comment |
3 Answers
3
active
oldest
votes
Maybe use:
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.rename("b.txt", "a.txt")
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
|
show 7 more comments
with open('a.txt', 'r') as istr, open('output.txt', 'w') as ostr:
for i, line in enumerate(istr):
# Get rid of the trailing newline (if any).
line = line.rstrip('n')
if i == 0:
line += 'Hello'
print(line, file=ostr)
You need to format your code, or better usewith open(...) as f, open(...) as f2:it's more compact.
– Chiheb Nexus
Jan 20 at 2:17
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
add a comment |
Here's an edited version of @U9-Forward's code that worked for me.
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.remove("a.txt")
os.rename("b.txt", "a.txt")
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should doremoveandrename.
– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to changewanda+tor+which just resulted in two lines.
– Dansey
Jan 20 at 7:08
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
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%2f54272934%2fhow-to-append-text-to-the-end-of-the-first-line%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe use:
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.rename("b.txt", "a.txt")
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
|
show 7 more comments
Maybe use:
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.rename("b.txt", "a.txt")
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
|
show 7 more comments
Maybe use:
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.rename("b.txt", "a.txt")
Maybe use:
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.rename("b.txt", "a.txt")
edited Jan 20 at 7:13
answered Jan 20 at 2:05
U9-ForwardU9-Forward
15.2k41438
15.2k41438
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
|
show 7 more comments
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@U9-Forward f.read() is coming up empty for some reason, any ideas?
– Dansey
Jan 20 at 6:28
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@Dansey You should only use my code, make my code the full code for it to work.
– U9-Forward
Jan 20 at 6:47
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@U9-Forward I've been troubleshooting this for quite a while, f.seek(0) still seems to return a blank read.
– Dansey
Jan 20 at 6:48
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@Dansey You should just copy and paste my code into an empty file, then it should work...
– U9-Forward
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
@U9-Forward, yes, I'm not using the previous code, read is empty.
– Dansey
Jan 20 at 6:49
|
show 7 more comments
with open('a.txt', 'r') as istr, open('output.txt', 'w') as ostr:
for i, line in enumerate(istr):
# Get rid of the trailing newline (if any).
line = line.rstrip('n')
if i == 0:
line += 'Hello'
print(line, file=ostr)
You need to format your code, or better usewith open(...) as f, open(...) as f2:it's more compact.
– Chiheb Nexus
Jan 20 at 2:17
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
add a comment |
with open('a.txt', 'r') as istr, open('output.txt', 'w') as ostr:
for i, line in enumerate(istr):
# Get rid of the trailing newline (if any).
line = line.rstrip('n')
if i == 0:
line += 'Hello'
print(line, file=ostr)
You need to format your code, or better usewith open(...) as f, open(...) as f2:it's more compact.
– Chiheb Nexus
Jan 20 at 2:17
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
add a comment |
with open('a.txt', 'r') as istr, open('output.txt', 'w') as ostr:
for i, line in enumerate(istr):
# Get rid of the trailing newline (if any).
line = line.rstrip('n')
if i == 0:
line += 'Hello'
print(line, file=ostr)
with open('a.txt', 'r') as istr, open('output.txt', 'w') as ostr:
for i, line in enumerate(istr):
# Get rid of the trailing newline (if any).
line = line.rstrip('n')
if i == 0:
line += 'Hello'
print(line, file=ostr)
edited Jan 20 at 2:26
answered Jan 20 at 1:59
mridmrid
3,96731334
3,96731334
You need to format your code, or better usewith open(...) as f, open(...) as f2:it's more compact.
– Chiheb Nexus
Jan 20 at 2:17
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
add a comment |
You need to format your code, or better usewith open(...) as f, open(...) as f2:it's more compact.
– Chiheb Nexus
Jan 20 at 2:17
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
You need to format your code, or better use
with open(...) as f, open(...) as f2: it's more compact.– Chiheb Nexus
Jan 20 at 2:17
You need to format your code, or better use
with open(...) as f, open(...) as f2: it's more compact.– Chiheb Nexus
Jan 20 at 2:17
1
1
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
@ChihebNexus Yup, i was ready to say that also.
– U9-Forward
Jan 20 at 2:19
add a comment |
Here's an edited version of @U9-Forward's code that worked for me.
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.remove("a.txt")
os.rename("b.txt", "a.txt")
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should doremoveandrename.
– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to changewanda+tor+which just resulted in two lines.
– Dansey
Jan 20 at 7:08
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
add a comment |
Here's an edited version of @U9-Forward's code that worked for me.
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.remove("a.txt")
os.rename("b.txt", "a.txt")
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should doremoveandrename.
– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to changewanda+tor+which just resulted in two lines.
– Dansey
Jan 20 at 7:08
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
add a comment |
Here's an edited version of @U9-Forward's code that worked for me.
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.remove("a.txt")
os.rename("b.txt", "a.txt")
Here's an edited version of @U9-Forward's code that worked for me.
with open ("a.txt", "r") as f, open ("b.txt", "w") as f2:
f2.write(f.read().rstrip()+"hello ")
os.remove("a.txt")
os.rename("b.txt", "a.txt")
answered Jan 20 at 7:03
DanseyDansey
716
716
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should doremoveandrename.
– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to changewanda+tor+which just resulted in two lines.
– Dansey
Jan 20 at 7:08
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
add a comment |
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should doremoveandrename.
– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to changewanda+tor+which just resulted in two lines.
– Dansey
Jan 20 at 7:08
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Any idea why this was required?
– Dansey
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
Oh yeah, i was ready to edit to this.
– U9-Forward
Jan 20 at 7:03
It's needed because you can't open a file twice. so you should do
remove and rename.– U9-Forward
Jan 20 at 7:04
It's needed because you can't open a file twice. so you should do
remove and rename.– U9-Forward
Jan 20 at 7:04
Got it, I figured that you couldn't open it twice so I had tried to change
w and a+ to r+ which just resulted in two lines.– Dansey
Jan 20 at 7:08
Got it, I figured that you couldn't open it twice so I had tried to change
w and a+ to r+ which just resulted in two lines.– Dansey
Jan 20 at 7:08
1
1
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
Yup, maybe you should accept your own answer in two days, but still, you won't get +15 since it is self-accepted.
– U9-Forward
Jan 20 at 7:09
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%2f54272934%2fhow-to-append-text-to-the-end-of-the-first-line%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
How did it not work?
– Mad Physicist
Jan 20 at 6:58
Are you trying to insert text into a line in an existing file?
– Mad Physicist
Jan 20 at 7:01