Skip to main content

Get Config Folder Path

Description

The get_config_folder_path function determines and creates the appropriate configuration folder path for an application, based on the operating system. It returns the full path to the config directory.

Function Signature:

def get_config_folder_path(app_name: str, base_folder_path: str = '') -> str:

Parameters

  • app_name (str): The name of the application. This will be used to generate the config folder name.
  • base_folder_path (str, optional): A custom base path used on Linux/Unix systems to determine where to create the config folder. If omitted, defaults to the user's home directory under ~/.config/.

Returns

  • str: The full path to the application's config folder. The folder will be created if it does not exist.

Example Usage

config_path = get_config_folder_path("MyCoolApp")
print(f"Config path: {config_path}")

Platform Behavior

  • Windows:
    • The config path is constructed using the AppData\Roaming directory for the current user:
      C:\Users\{Username}\AppData\Roaming\{AppName}\config
  • Linux/macOS:
    • If base_folder_path is provided, the config path will be:
      {base_folder_path}/.config/{app_name}
    • Otherwise, it defaults to:
      ~/.config/{app_name}

Notes

  • The function ensures that the configuration directory exists by creating it if it doesn't.
  • It uses os.getlogin() to determine the current user on Windows, and os.path.expanduser() on Unix-like systems.

Error Handling

  • If directory creation fails, an exception will be raised by os.makedirs().