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?

0

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.