'python file creation is giving some error

result={'name':12,'age':23}
file_name="SpaceX_"+date_format+"_data"+".json"
# load into a file
json.dump( result, open( file_name, 'w' ) )

I get this error json.dump( result, open( file_name, 'w' ) ) FileNotFoundError: [Errno 2] No such file or directory: 'SpaceX_03/21/22_data.json'



Solution 1:[1]

This should work:

import json
from datetime import date

today = date.today()
date_format = today.strftime("%m.%d.%y")
result = {'name': 12, 'age': 23}
file_name = "SpaceX_"+date_format+"_data"+".json"  # load into a file with

with open(file_name, "x") as f:
    json.dump(result, f)

Your issue was on this line date_format = today.strftime("%m/%d/%y"). As / was tring to go down a directory (or into a new folder). E.g the path folder1/folder2 is saying to go to folder1 then to folder2 which is in folder1. Which was fixed by changing / to . in your date string

Just for the future, remember to avoid / in file names completely

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1