2

I am saving json file from my code using os.mkdir(path) which creates the json folder containing json file, now I want to store only json file to the folder without creating the folder of json containing file, please help me, thanks in advance

cwd=os.getcwd()#current directory
os.chdir(cwd)
#print(cwd)
path=cwd+'/'+json_folder_name
if  os.path.isdir(path):
    shutil.rmtree(path)
os.mkdir(json_folder_name)
2
  • Where's the code where you write the json file to storage os.mkdir is specifically just for making a folder Commented Mar 16, 2021 at 12:28
  • Or is that what you're wondering, how to write a json file in the first place Commented Mar 16, 2021 at 12:29

1 Answer 1

2

You can open a file handle and use json.dump.

import json

with open("/path/to/file.json", "w+") as f:
    json.dump(object_to_write, f)
Sign up to request clarification or add additional context in comments.

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.