writting to a file , and save one












0















I would like why when I tried to save my tokens to my save always save the first one



#!/usr/bin/python
import random
import string

count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','w') as keys:
keys.write(key)
count += 1









share|improve this question


















  • 2





    everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

    – iElden
    Nov 23 '18 at 11:08













  • I want to save my 5 rounds from my while to my file , but only save the first one

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:10






  • 1





    no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

    – iElden
    Nov 23 '18 at 11:13











  • Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

    – PM 2Ring
    Nov 23 '18 at 11:36











  • I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:40
















0















I would like why when I tried to save my tokens to my save always save the first one



#!/usr/bin/python
import random
import string

count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','w') as keys:
keys.write(key)
count += 1









share|improve this question


















  • 2





    everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

    – iElden
    Nov 23 '18 at 11:08













  • I want to save my 5 rounds from my while to my file , but only save the first one

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:10






  • 1





    no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

    – iElden
    Nov 23 '18 at 11:13











  • Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

    – PM 2Ring
    Nov 23 '18 at 11:36











  • I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:40














0












0








0








I would like why when I tried to save my tokens to my save always save the first one



#!/usr/bin/python
import random
import string

count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','w') as keys:
keys.write(key)
count += 1









share|improve this question














I would like why when I tried to save my tokens to my save always save the first one



#!/usr/bin/python
import random
import string

count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','w') as keys:
keys.write(key)
count += 1






python






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 11:05









m4st3rRul3z m4st3rRul3zm4st3rRul3z m4st3rRul3z

94




94








  • 2





    everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

    – iElden
    Nov 23 '18 at 11:08













  • I want to save my 5 rounds from my while to my file , but only save the first one

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:10






  • 1





    no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

    – iElden
    Nov 23 '18 at 11:13











  • Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

    – PM 2Ring
    Nov 23 '18 at 11:36











  • I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:40














  • 2





    everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

    – iElden
    Nov 23 '18 at 11:08













  • I want to save my 5 rounds from my while to my file , but only save the first one

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:10






  • 1





    no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

    – iElden
    Nov 23 '18 at 11:13











  • Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

    – PM 2Ring
    Nov 23 '18 at 11:36











  • I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

    – m4st3rRul3z m4st3rRul3z
    Nov 23 '18 at 11:40








2




2





everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

– iElden
Nov 23 '18 at 11:08







everytime you open your keys file, you delete all content in your file. I do not know what you want to do so I can't help you more.

– iElden
Nov 23 '18 at 11:08















I want to save my 5 rounds from my while to my file , but only save the first one

– m4st3rRul3z m4st3rRul3z
Nov 23 '18 at 11:10





I want to save my 5 rounds from my while to my file , but only save the first one

– m4st3rRul3z m4st3rRul3z
Nov 23 '18 at 11:10




1




1





no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

– iElden
Nov 23 '18 at 11:13





no, it's only save the last. if you want to do this, you can initialise a list, append it every loop and save in your keys file outside the loop

– iElden
Nov 23 '18 at 11:13













Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

– PM 2Ring
Nov 23 '18 at 11:36





Do you really want all the output to be on a single line like that? Or do you want each key string to be written to a separate line?

– PM 2Ring
Nov 23 '18 at 11:36













I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

– m4st3rRul3z m4st3rRul3z
Nov 23 '18 at 11:40





I fix it , but for example if I have the first 5 added how can I check which ones are the new keys in the same file?

– m4st3rRul3z m4st3rRul3z
Nov 23 '18 at 11:40












2 Answers
2






active

oldest

votes


















0














You need to append the the file than to write it every time i.e. use 'a' instead of 'w' in open as -



#!/usr/bin/python
import random
import string

count = 1
while count <= 5:
t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
key = str(''.join(random.sample(t,33)))
with open('keys','a') as keys:
keys.write(key)
count += 1


You may also need to add newline after the each key.




always save the first one




No, your code doesn't saves the first key, but instead the last key generated.






share|improve this answer































    0














    #!/usr/bin/python
    import random
    import string

    count = 1
    with open('keys','w') as keys:
    while count <= 5:
    t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
    key = str(''.join(random.sample(t,33)))
    keys.write(key)
    count += 1





    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%2f53445521%2fwritting-to-a-file-and-save-one%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You need to append the the file than to write it every time i.e. use 'a' instead of 'w' in open as -



      #!/usr/bin/python
      import random
      import string

      count = 1
      while count <= 5:
      t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
      key = str(''.join(random.sample(t,33)))
      with open('keys','a') as keys:
      keys.write(key)
      count += 1


      You may also need to add newline after the each key.




      always save the first one




      No, your code doesn't saves the first key, but instead the last key generated.






      share|improve this answer




























        0














        You need to append the the file than to write it every time i.e. use 'a' instead of 'w' in open as -



        #!/usr/bin/python
        import random
        import string

        count = 1
        while count <= 5:
        t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
        key = str(''.join(random.sample(t,33)))
        with open('keys','a') as keys:
        keys.write(key)
        count += 1


        You may also need to add newline after the each key.




        always save the first one




        No, your code doesn't saves the first key, but instead the last key generated.






        share|improve this answer


























          0












          0








          0







          You need to append the the file than to write it every time i.e. use 'a' instead of 'w' in open as -



          #!/usr/bin/python
          import random
          import string

          count = 1
          while count <= 5:
          t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
          key = str(''.join(random.sample(t,33)))
          with open('keys','a') as keys:
          keys.write(key)
          count += 1


          You may also need to add newline after the each key.




          always save the first one




          No, your code doesn't saves the first key, but instead the last key generated.






          share|improve this answer













          You need to append the the file than to write it every time i.e. use 'a' instead of 'w' in open as -



          #!/usr/bin/python
          import random
          import string

          count = 1
          while count <= 5:
          t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
          key = str(''.join(random.sample(t,33)))
          with open('keys','a') as keys:
          keys.write(key)
          count += 1


          You may also need to add newline after the each key.




          always save the first one




          No, your code doesn't saves the first key, but instead the last key generated.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 23 '18 at 11:12









          Anmol GautamAnmol Gautam

          4891720




          4891720

























              0














              #!/usr/bin/python
              import random
              import string

              count = 1
              with open('keys','w') as keys:
              while count <= 5:
              t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
              key = str(''.join(random.sample(t,33)))
              keys.write(key)
              count += 1





              share|improve this answer






























                0














                #!/usr/bin/python
                import random
                import string

                count = 1
                with open('keys','w') as keys:
                while count <= 5:
                t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
                key = str(''.join(random.sample(t,33)))
                keys.write(key)
                count += 1





                share|improve this answer




























                  0












                  0








                  0







                  #!/usr/bin/python
                  import random
                  import string

                  count = 1
                  with open('keys','w') as keys:
                  while count <= 5:
                  t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
                  key = str(''.join(random.sample(t,33)))
                  keys.write(key)
                  count += 1





                  share|improve this answer















                  #!/usr/bin/python
                  import random
                  import string

                  count = 1
                  with open('keys','w') as keys:
                  while count <= 5:
                  t = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation
                  key = str(''.join(random.sample(t,33)))
                  keys.write(key)
                  count += 1






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 23 '18 at 13:33

























                  answered Nov 23 '18 at 11:12









                  duyueduyue

                  47529




                  47529






























                      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%2f53445521%2fwritting-to-a-file-and-save-one%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







                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()