Write mode “a” in python





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-5















when i use the write mode:



d = open("test.txt","a")
d.write(letter)


it always appends the text at the end everytime i write someting into the file.
My question is, how to write as new line like this:



a
b
c


Currently it writes:



abc


Using n does not work unfortunally...



Thanks so much










share|improve this question




















  • 3





    What code do you have already that writes to the file object d?

    – Adam
    Jan 3 at 22:44






  • 4





    /n is not n.

    – user2357112
    Jan 3 at 22:45











  • i just edited the code

    – ddd
    Jan 3 at 22:47











  • ...and how are you creating the variable letter?

    – Adam
    Jan 3 at 22:48











  • So... are you using n or /n?

    – Ender Look
    Jan 3 at 22:50


















-5















when i use the write mode:



d = open("test.txt","a")
d.write(letter)


it always appends the text at the end everytime i write someting into the file.
My question is, how to write as new line like this:



a
b
c


Currently it writes:



abc


Using n does not work unfortunally...



Thanks so much










share|improve this question




















  • 3





    What code do you have already that writes to the file object d?

    – Adam
    Jan 3 at 22:44






  • 4





    /n is not n.

    – user2357112
    Jan 3 at 22:45











  • i just edited the code

    – ddd
    Jan 3 at 22:47











  • ...and how are you creating the variable letter?

    – Adam
    Jan 3 at 22:48











  • So... are you using n or /n?

    – Ender Look
    Jan 3 at 22:50














-5












-5








-5








when i use the write mode:



d = open("test.txt","a")
d.write(letter)


it always appends the text at the end everytime i write someting into the file.
My question is, how to write as new line like this:



a
b
c


Currently it writes:



abc


Using n does not work unfortunally...



Thanks so much










share|improve this question
















when i use the write mode:



d = open("test.txt","a")
d.write(letter)


it always appends the text at the end everytime i write someting into the file.
My question is, how to write as new line like this:



a
b
c


Currently it writes:



abc


Using n does not work unfortunally...



Thanks so much







python arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 22:45







ddd

















asked Jan 3 at 22:43









dddddd

375




375








  • 3





    What code do you have already that writes to the file object d?

    – Adam
    Jan 3 at 22:44






  • 4





    /n is not n.

    – user2357112
    Jan 3 at 22:45











  • i just edited the code

    – ddd
    Jan 3 at 22:47











  • ...and how are you creating the variable letter?

    – Adam
    Jan 3 at 22:48











  • So... are you using n or /n?

    – Ender Look
    Jan 3 at 22:50














  • 3





    What code do you have already that writes to the file object d?

    – Adam
    Jan 3 at 22:44






  • 4





    /n is not n.

    – user2357112
    Jan 3 at 22:45











  • i just edited the code

    – ddd
    Jan 3 at 22:47











  • ...and how are you creating the variable letter?

    – Adam
    Jan 3 at 22:48











  • So... are you using n or /n?

    – Ender Look
    Jan 3 at 22:50








3




3





What code do you have already that writes to the file object d?

– Adam
Jan 3 at 22:44





What code do you have already that writes to the file object d?

– Adam
Jan 3 at 22:44




4




4





/n is not n.

– user2357112
Jan 3 at 22:45





/n is not n.

– user2357112
Jan 3 at 22:45













i just edited the code

– ddd
Jan 3 at 22:47





i just edited the code

– ddd
Jan 3 at 22:47













...and how are you creating the variable letter?

– Adam
Jan 3 at 22:48





...and how are you creating the variable letter?

– Adam
Jan 3 at 22:48













So... are you using n or /n?

– Ender Look
Jan 3 at 22:50





So... are you using n or /n?

– Ender Look
Jan 3 at 22:50












3 Answers
3






active

oldest

votes


















2














You can either add a newline character after the letter:



d.write(letter + 'n')


Or if you are using Python 3 or if you from __future__ import print_function in Python 2:



print(letter, file=d)





share|improve this answer































    1














    As mentioned here, you can do :



    with open('test.txt', 'a') as d:

    d.write('YOURTEXTn')





    share|improve this answer































      0














      The new-line character "n" works fine with "a" (append) option for writing files in python.



      Example:



      d = open(filepath, "a")
      d.write("hello")
      d.write("n")
      d.write("hello again")
      d.close()


      o/p:



      hello
      hello again


      OR -> To append text from next line of the existing text of file.



      d.write('n')
      d.write("hello" + 'n')





      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%2f54030824%2fwrite-mode-a-in-python%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









        2














        You can either add a newline character after the letter:



        d.write(letter + 'n')


        Or if you are using Python 3 or if you from __future__ import print_function in Python 2:



        print(letter, file=d)





        share|improve this answer




























          2














          You can either add a newline character after the letter:



          d.write(letter + 'n')


          Or if you are using Python 3 or if you from __future__ import print_function in Python 2:



          print(letter, file=d)





          share|improve this answer


























            2












            2








            2







            You can either add a newline character after the letter:



            d.write(letter + 'n')


            Or if you are using Python 3 or if you from __future__ import print_function in Python 2:



            print(letter, file=d)





            share|improve this answer













            You can either add a newline character after the letter:



            d.write(letter + 'n')


            Or if you are using Python 3 or if you from __future__ import print_function in Python 2:



            print(letter, file=d)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 22:49









            blhsingblhsing

            42.8k41743




            42.8k41743

























                1














                As mentioned here, you can do :



                with open('test.txt', 'a') as d:

                d.write('YOURTEXTn')





                share|improve this answer




























                  1














                  As mentioned here, you can do :



                  with open('test.txt', 'a') as d:

                  d.write('YOURTEXTn')





                  share|improve this answer


























                    1












                    1








                    1







                    As mentioned here, you can do :



                    with open('test.txt', 'a') as d:

                    d.write('YOURTEXTn')





                    share|improve this answer













                    As mentioned here, you can do :



                    with open('test.txt', 'a') as d:

                    d.write('YOURTEXTn')






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 22:56









                    The white rabbitThe white rabbit

                    92




                    92























                        0














                        The new-line character "n" works fine with "a" (append) option for writing files in python.



                        Example:



                        d = open(filepath, "a")
                        d.write("hello")
                        d.write("n")
                        d.write("hello again")
                        d.close()


                        o/p:



                        hello
                        hello again


                        OR -> To append text from next line of the existing text of file.



                        d.write('n')
                        d.write("hello" + 'n')





                        share|improve this answer






























                          0














                          The new-line character "n" works fine with "a" (append) option for writing files in python.



                          Example:



                          d = open(filepath, "a")
                          d.write("hello")
                          d.write("n")
                          d.write("hello again")
                          d.close()


                          o/p:



                          hello
                          hello again


                          OR -> To append text from next line of the existing text of file.



                          d.write('n')
                          d.write("hello" + 'n')





                          share|improve this answer




























                            0












                            0








                            0







                            The new-line character "n" works fine with "a" (append) option for writing files in python.



                            Example:



                            d = open(filepath, "a")
                            d.write("hello")
                            d.write("n")
                            d.write("hello again")
                            d.close()


                            o/p:



                            hello
                            hello again


                            OR -> To append text from next line of the existing text of file.



                            d.write('n')
                            d.write("hello" + 'n')





                            share|improve this answer















                            The new-line character "n" works fine with "a" (append) option for writing files in python.



                            Example:



                            d = open(filepath, "a")
                            d.write("hello")
                            d.write("n")
                            d.write("hello again")
                            d.close()


                            o/p:



                            hello
                            hello again


                            OR -> To append text from next line of the existing text of file.



                            d.write('n')
                            d.write("hello" + 'n')






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 3 at 23:09

























                            answered Jan 3 at 23:03









                            Ramanpreet SinghRamanpreet Singh

                            134




                            134






























                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54030824%2fwrite-mode-a-in-python%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

                                Monofisismo

                                Angular Downloading a file using contenturl with Basic Authentication

                                Olmecas