-
Notifications
You must be signed in to change notification settings - Fork 0
save
sryu1 edited this page Jan 4, 2023
·
2 revisions
Save variables or a dictionary to a JSON file.
-
config_file(str): The file to save the data to. -
**kwargs(Any): The data to save. If a dictionary is passed as the only argument, it will be saved to the JSON file as is. If multiple keyword arguments are passed, they will be combined into a dictionary and saved to the JSON file.
-
FileNotFoundError: If the directory forconfig_filedoes not exist. -
PermissionError: If the file cannot be opened for writing due to insufficient permissions. -
IOError: If there is an error writing to the file.
# Save a dictionary to a JSON file
settings = {"foo": 1, "bar": "hello"}
save("settings.json", **settings)
# Save variables to a JSON file
foo = 1
bar = "hello"
save("settings.json", foo=foo, bar=bar)
# Save multiple dictionaries to a JSON file
settings1 = {"foo": 1, "bar": "hello"}
settings2 = {"baz": 2, "qux": "world"}
save("settings.json", **settings1, **settings2)