Announcing config-path an OS independent configuration file paths

Barry Scott barry at barrys-emacs.org
Mon Sep 30 08:12:44 EDT 2019


See https://pypi.org/project/config-path/ for documentation.

Install using pip:

	python -m pip install config-path

I write tools that run on macOS, Unix and Windows systems.
Each operating system has its own conventions on where config
files should be stored. Following the conventions is not always
straight forward.

After a question raised here on Python users about config file locations
I was inspired to write this module to help with that problem.

config_path library works out which path to use for configuration folders
and files in an operating system independent way.

Each operating system has particular conventions for where an application
is expected to stores it configuration. The information provided to ConfigPath
is used to figure out an appropriate file path or folder path for the application's
configuration data.

Supports Windows, macOS and most unix systems using the 'XDG Base Directory Specification'.

Example for the "widget" app from "example.com" that uses JSON for its config:

from config_path import ConfigPath
conf_path = ConfigPath( 'example.com', 'widget', '.json' )

path = conf_path.saveFilePath( mkdir=True )
with path.open() as f:
	f.write( config_data )

And to read the config:

path = conf_path.readFilePath()
if path is not None:
	# path exists and config can be read
	with path.open() as f:
		config = json.loads( f )
else:
	config = default_config

Barry





More information about the Python-list mailing list