Skip to main content

Set Config Value

Description

The set_config_value function writes a string value to a named configuration file within a specified folder. If the folder does not exist, it is created automatically.

Function Signature:

def set_config_value(config_folder_path: str, name: str, value: str) -> bool:

Parameters

  • config_folder_path (str): Path to the folder where the config file should be saved.
  • name (str): The name of the config file (without .txt extension).
  • value (str): The string value to write into the file.

Returns

  • bool: Returns True if the value is successfully written, False if an error occurs.

Example Usage

config_path = get_config_folder_path("MyCoolApp")
success = set_config_value(config_path, "api_key", "new-api-key-123")
if success:
print("API key updated.")

Behavior

  • Ensures the directory exists before attempting to write.
  • Overwrites the config file if it already exists.
  • Prints a confirmation message on success.

Error Handling

  • Catches and prints any exceptions that occur during the write operation.
  • Returns False if an exception is raised, otherwise returns True.