Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

XDG_CONFIG_HOME for Python

Here is a python oneliner for loading an application config from the recommended path, because I've needed it at least twice now, and I'll need it again in the future.

# Use APPNAME_CONF file if defined, otherwise, use $XDG_CONFIG_HOME/appname.conf, and use ~/.config if XDG_CONFIG_HOME is not defined
conffile = os.environ.get("APPNAME_CONF",os.path.join(os.environ.get("XDG_CONFIG_HOME",os.path.join(os.getenv("HOME"),".config")),"appname.conf"))

Using configparser is left as an exercise for the reader.

Comments