f.write fails to record inputs
up vote
-2
down vote
favorite
Suppose such a text
In [134]: !cat test1.md
The first test
I open it and write contents
In [156]: f1 = open("test1.md", "r+")
In [157]: f1.write("The second test")
Out[157]: 15
In [158]: !cat test1.md
The first test
test second method
It works correctly until now, if continue to input
In [159]: f1.write("The third test")
Out[159]: 14
In [160]: !cat test1.md
The first test
test second method
In [161]: f1.write("The fourth test")
Out[161]: 16
In [162]: !cat test1.md
The first test
test second method
What's the problem with f.write
?.
python
add a comment |
up vote
-2
down vote
favorite
Suppose such a text
In [134]: !cat test1.md
The first test
I open it and write contents
In [156]: f1 = open("test1.md", "r+")
In [157]: f1.write("The second test")
Out[157]: 15
In [158]: !cat test1.md
The first test
test second method
It works correctly until now, if continue to input
In [159]: f1.write("The third test")
Out[159]: 14
In [160]: !cat test1.md
The first test
test second method
In [161]: f1.write("The fourth test")
Out[161]: 16
In [162]: !cat test1.md
The first test
test second method
What's the problem with f.write
?.
python
1
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
2
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Tryf.close()
?
– Mayank Porwal
Nov 11 at 6:16
1
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
Suppose such a text
In [134]: !cat test1.md
The first test
I open it and write contents
In [156]: f1 = open("test1.md", "r+")
In [157]: f1.write("The second test")
Out[157]: 15
In [158]: !cat test1.md
The first test
test second method
It works correctly until now, if continue to input
In [159]: f1.write("The third test")
Out[159]: 14
In [160]: !cat test1.md
The first test
test second method
In [161]: f1.write("The fourth test")
Out[161]: 16
In [162]: !cat test1.md
The first test
test second method
What's the problem with f.write
?.
python
Suppose such a text
In [134]: !cat test1.md
The first test
I open it and write contents
In [156]: f1 = open("test1.md", "r+")
In [157]: f1.write("The second test")
Out[157]: 15
In [158]: !cat test1.md
The first test
test second method
It works correctly until now, if continue to input
In [159]: f1.write("The third test")
Out[159]: 14
In [160]: !cat test1.md
The first test
test second method
In [161]: f1.write("The fourth test")
Out[161]: 16
In [162]: !cat test1.md
The first test
test second method
What's the problem with f.write
?.
python
python
edited Nov 11 at 9:56
Md. Mokammal Hossen Farnan
583320
583320
asked Nov 11 at 6:11
avirate
647113
647113
1
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
2
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Tryf.close()
?
– Mayank Porwal
Nov 11 at 6:16
1
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27
add a comment |
1
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
2
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Tryf.close()
?
– Mayank Porwal
Nov 11 at 6:16
1
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27
1
1
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
2
2
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Try
f.close()
?– Mayank Porwal
Nov 11 at 6:16
Try
f.close()
?– Mayank Porwal
Nov 11 at 6:16
1
1
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
You need to flush()
the buffer then close()
the file. Also maybe append your strings to the file.
f1 = open("test1.md", "a") # "a" means append to file
f1.write("The second test")
f1.flush()
f1.close()
Thecat
the file;
cat test1.md
The first test
test second method
The Correcct way.
You should really be doing it like this;
with open("test1.md", "a") as f1:
f1.write("The second test")
f1.flush()
f1.close()
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
You need to flush()
the buffer then close()
the file. Also maybe append your strings to the file.
f1 = open("test1.md", "a") # "a" means append to file
f1.write("The second test")
f1.flush()
f1.close()
Thecat
the file;
cat test1.md
The first test
test second method
The Correcct way.
You should really be doing it like this;
with open("test1.md", "a") as f1:
f1.write("The second test")
f1.flush()
f1.close()
add a comment |
up vote
0
down vote
You need to flush()
the buffer then close()
the file. Also maybe append your strings to the file.
f1 = open("test1.md", "a") # "a" means append to file
f1.write("The second test")
f1.flush()
f1.close()
Thecat
the file;
cat test1.md
The first test
test second method
The Correcct way.
You should really be doing it like this;
with open("test1.md", "a") as f1:
f1.write("The second test")
f1.flush()
f1.close()
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to flush()
the buffer then close()
the file. Also maybe append your strings to the file.
f1 = open("test1.md", "a") # "a" means append to file
f1.write("The second test")
f1.flush()
f1.close()
Thecat
the file;
cat test1.md
The first test
test second method
The Correcct way.
You should really be doing it like this;
with open("test1.md", "a") as f1:
f1.write("The second test")
f1.flush()
f1.close()
You need to flush()
the buffer then close()
the file. Also maybe append your strings to the file.
f1 = open("test1.md", "a") # "a" means append to file
f1.write("The second test")
f1.flush()
f1.close()
Thecat
the file;
cat test1.md
The first test
test second method
The Correcct way.
You should really be doing it like this;
with open("test1.md", "a") as f1:
f1.write("The second test")
f1.flush()
f1.close()
answered Nov 11 at 9:48
Jack Herer
319111
319111
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53246317%2ff-write-fails-to-record-inputs%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
1
try f.flush() ?
– Abhijith Shivaswamy
Nov 11 at 6:15
2
Where did "test second method" come from?
– user2357112
Nov 11 at 6:15
Try
f.close()
?– Mayank Porwal
Nov 11 at 6:16
1
Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhijith Shivaswamy
Nov 11 at 6:19
thanks for your kindness @AbhijithShivaswamy
– avirate
Nov 11 at 6:27