f.write fails to record inputs










-2














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() ?
    – Abhi
    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
    – Abhi
    Nov 11 at 6:19










  • thanks for your kindness @AbhijithShivaswamy
    – JawSaw
    Nov 11 at 6:27















-2














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() ?
    – Abhi
    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
    – Abhi
    Nov 11 at 6:19










  • thanks for your kindness @AbhijithShivaswamy
    – JawSaw
    Nov 11 at 6:27













-2












-2








-2







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

585320




585320










asked Nov 11 at 6:11









JawSaw

4,02811633




4,02811633







  • 1




    try f.flush() ?
    – Abhi
    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
    – Abhi
    Nov 11 at 6:19










  • thanks for your kindness @AbhijithShivaswamy
    – JawSaw
    Nov 11 at 6:27












  • 1




    try f.flush() ?
    – Abhi
    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
    – Abhi
    Nov 11 at 6:19










  • thanks for your kindness @AbhijithShivaswamy
    – JawSaw
    Nov 11 at 6:27







1




1




try f.flush() ?
– Abhi
Nov 11 at 6:15




try f.flush() ?
– Abhi
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
– Abhi
Nov 11 at 6:19




Check out this, might help tutorialspoint.com/python/file_flush.htm
– Abhi
Nov 11 at 6:19












thanks for your kindness @AbhijithShivaswamy
– JawSaw
Nov 11 at 6:27




thanks for your kindness @AbhijithShivaswamy
– JawSaw
Nov 11 at 6:27












1 Answer
1






active

oldest

votes


















0














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',
    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%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









    0














    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

























      0














      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























        0












        0








        0






        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

        315112




        315112



























            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







            Popular posts from this blog

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo