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?.










share|improve this question



















  • 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














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?.










share|improve this question



















  • 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












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?.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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










  • 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












  • 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







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












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





share|improve this answer




















    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',
    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%2f53246317%2ff-write-fails-to-record-inputs%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








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





    share|improve this answer
























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





      share|improve this answer






















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





        share|improve this answer












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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 9:48









        Jack Herer

        319111




        319111



























            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.





            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.




            draft saved


            draft discarded














            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





















































            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 to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto