Skip to content
sryu1 edited this page Jan 4, 2023 · 2 revisions

save function

Save variables or a dictionary to a JSON file.

Parameters

  • 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.

Raises

  • FileNotFoundError: If the directory for config_file does 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.

Example

# 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)
Clone this wiki locally