Problem writing JSON to file using PythonHow do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Can comments be used in JSON?How can I pretty-print JSON in a shell script?Does Python have a ternary conditional operator?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How do I list all files of a directory?Does Python have a string 'contains' substring method?

Is the apartment I want to rent a scam?

How important is a good quality camera for good photography?

Why are angular mometum and angular velocity not necessarily parallel, but linear momentum and linear velocity are always parallel?

Can GPL and BSD licensed applications be used for government work?

How can I make sure my players' decisions have consequences?

Is there a published campaign where a missing artifact or a relic is creating trouble by its absence?

Very basic singly linked list

Is a normal-sized rug with the Animate Objects spell cast on it able to carry a person and fly?

Who has jurisdiction for a crime committed in an embassy?

Monty Hall Problem with a Fallible Monty

"I you already know": is this proper English?

What exactly makes a General Products hull nearly indestructible?

How can I create a shape in Illustrator which follows a path in descending order size?

Impact of throwing away fruit waste on a peak > 3200 m above a glacier

What is the purpose of the fuel shutoff valve?

What is "ass door"?

Why keep the bed heated after initial layer(s) with PLA (or PETG)?

Why did computer video outputs go from digital to analog, then back to digital?

What should I say when a company asks you why someone (a friend) who was fired left?

Where is this photo of a group of hikers taken? Is it really in the Ural?

How did C64 games handle music during gameplay?

Other than a swing wing, what types of variable geometry have flown?

kids pooling money for Lego League and taxes

What is a reasonable time for modern human society to adapt to dungeons?



Problem writing JSON to file using Python


How do I check whether a file exists without exceptions?Calling an external command in PythonWhat are metaclasses in Python?Can comments be used in JSON?How can I pretty-print JSON in a shell script?Does Python have a ternary conditional operator?What is the correct JSON content type?Why does Google prepend while(1); to their JSON responses?How do I list all files of a directory?Does Python have a string 'contains' substring method?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to write a JSON file to the file system using Python. When I go to read the file there's nothing there. I think I'm doing something else wrong.



This is my code:



 today = datetime.today()
output_dir = "../../../json/iam"
output_file = output_dir + 'pol-aws-secrets-manager-' + user_name + today +'.json'
policy_doc = "blah":"blah"
with open(output_file, 'w+') as writer:
json.dump(policy_doc,writer)


What am I doing wrong?










share|improve this question






























    0















    I am trying to write a JSON file to the file system using Python. When I go to read the file there's nothing there. I think I'm doing something else wrong.



    This is my code:



     today = datetime.today()
    output_dir = "../../../json/iam"
    output_file = output_dir + 'pol-aws-secrets-manager-' + user_name + today +'.json'
    policy_doc = "blah":"blah"
    with open(output_file, 'w+') as writer:
    json.dump(policy_doc,writer)


    What am I doing wrong?










    share|improve this question


























      0












      0








      0








      I am trying to write a JSON file to the file system using Python. When I go to read the file there's nothing there. I think I'm doing something else wrong.



      This is my code:



       today = datetime.today()
      output_dir = "../../../json/iam"
      output_file = output_dir + 'pol-aws-secrets-manager-' + user_name + today +'.json'
      policy_doc = "blah":"blah"
      with open(output_file, 'w+') as writer:
      json.dump(policy_doc,writer)


      What am I doing wrong?










      share|improve this question
















      I am trying to write a JSON file to the file system using Python. When I go to read the file there's nothing there. I think I'm doing something else wrong.



      This is my code:



       today = datetime.today()
      output_dir = "../../../json/iam"
      output_file = output_dir + 'pol-aws-secrets-manager-' + user_name + today +'.json'
      policy_doc = "blah":"blah"
      with open(output_file, 'w+') as writer:
      json.dump(policy_doc,writer)


      What am I doing wrong?







      python json






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 26 at 15:59









      Chris

      64.1k18 gold badges132 silver badges125 bronze badges




      64.1k18 gold badges132 silver badges125 bronze badges










      asked Mar 26 at 15:49









      bluethundrbluethundr

      6625 gold badges19 silver badges44 bronze badges




      6625 gold badges19 silver badges44 bronze badges






















          2 Answers
          2






          active

          oldest

          votes


















          5














          Take a look in ../../../json/ for your files.



          You're building your filenames by sticking strings together, which is very error prone. In this case you've forgotten a /, so instead of getting files like



          ../../../json/iam/foo.json


          you're getting files like



          ../../../json/iamfoo.json


          A much safer strategy would be to use something like os.path or pathlib.






          share|improve this answer






























            2














            In addition to the response above. I tried your code and it created a file with "blah":"blah" without any issues.



            The problem could be in the path you specified.






            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%2f55361258%2fproblem-writing-json-to-file-using-python%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









              5














              Take a look in ../../../json/ for your files.



              You're building your filenames by sticking strings together, which is very error prone. In this case you've forgotten a /, so instead of getting files like



              ../../../json/iam/foo.json


              you're getting files like



              ../../../json/iamfoo.json


              A much safer strategy would be to use something like os.path or pathlib.






              share|improve this answer



























                5














                Take a look in ../../../json/ for your files.



                You're building your filenames by sticking strings together, which is very error prone. In this case you've forgotten a /, so instead of getting files like



                ../../../json/iam/foo.json


                you're getting files like



                ../../../json/iamfoo.json


                A much safer strategy would be to use something like os.path or pathlib.






                share|improve this answer

























                  5












                  5








                  5







                  Take a look in ../../../json/ for your files.



                  You're building your filenames by sticking strings together, which is very error prone. In this case you've forgotten a /, so instead of getting files like



                  ../../../json/iam/foo.json


                  you're getting files like



                  ../../../json/iamfoo.json


                  A much safer strategy would be to use something like os.path or pathlib.






                  share|improve this answer













                  Take a look in ../../../json/ for your files.



                  You're building your filenames by sticking strings together, which is very error prone. In this case you've forgotten a /, so instead of getting files like



                  ../../../json/iam/foo.json


                  you're getting files like



                  ../../../json/iamfoo.json


                  A much safer strategy would be to use something like os.path or pathlib.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 26 at 15:56









                  ChrisChris

                  64.1k18 gold badges132 silver badges125 bronze badges




                  64.1k18 gold badges132 silver badges125 bronze badges























                      2














                      In addition to the response above. I tried your code and it created a file with "blah":"blah" without any issues.



                      The problem could be in the path you specified.






                      share|improve this answer



























                        2














                        In addition to the response above. I tried your code and it created a file with "blah":"blah" without any issues.



                        The problem could be in the path you specified.






                        share|improve this answer

























                          2












                          2








                          2







                          In addition to the response above. I tried your code and it created a file with "blah":"blah" without any issues.



                          The problem could be in the path you specified.






                          share|improve this answer













                          In addition to the response above. I tried your code and it created a file with "blah":"blah" without any issues.



                          The problem could be in the path you specified.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 26 at 15:58









                          IrekIrek

                          1129 bronze badges




                          1129 bronze badges



























                              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%2f55361258%2fproblem-writing-json-to-file-using-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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript