4

My program needs to store some configuration files. The major operating systems seems to have a designated location to place those; for instance, on Freedesktop.org compliant systems, it will be the path stored in the $XDG_CONFIG_HOME environment variable.

Is there a method (or a library) to obtain this configuration home directory across the major operating systems: Windows, OS X, Linux?

Dun Peal
  • 12,539
  • 11
  • 27
  • 40
  • 1
    Your question is stated better but you can find better answers here : [Loading a config file from operation system independent place in python](https://stackoverflow.com/questions/3250164/loading-a-config-file-from-operation-system-independent-place-in-python/3250952#3250952) – Cyril Waechter Oct 01 '20 at 17:56
  • Does this answer your question? [Loading a config file from operation system independent place in python](https://stackoverflow.com/questions/3250164/loading-a-config-file-from-operation-system-independent-place-in-python) – zezollo Feb 15 '21 at 14:01
  • As of now, the best answer to this question is there: https://stackoverflow.com/a/63699709/3926735; though the next ones are good ones too. – zezollo Feb 15 '21 at 14:07

2 Answers2

2

You can use the package appdirs. This is developed by ActiveState who must have quite a lot of experience on cross-platform python.

import appdirs
appdirs.user_config_dir(appname='MyApp')

The package is just a single file (/module), so if you need it for a small script it is simple to just copy what you need. Otherwise the package is available with both pip and conda.

Janus
  • 4,944
  • 22
  • 31
0
import os

print os.path.expanduser("~/.my_config/data")

will always allow you to write ... config files are best stored where you want them

oftentimes on windows this is

os.path.expandvars("%appdata%/.my_config")

most other systems tend to put the in the userprofile director (~)

unless you are looking for a specific configuration file location(perhaps to interact with another software... in which case we would need to know which software)

Joran Beasley
  • 93,863
  • 11
  • 131
  • 160
  • 2
    The issue is that `~/.my_config` is not the standard location for configuration files. This implies several downsides. For instance, if I'm the user, even a power user, I won't be able to locate / properly reset / back-up that configuration. I'm trying to obtain the proper, standard path to place configuration, and yes, in some versions of windows that will be %appdata%, and OS X probably uses yet another location, but the point of the question is to get whatever location it is, across all major platforms, with (ideally) one function call. – Dun Peal Nov 03 '14 at 20:46
  • how is that not the standard? many professional softwares place it there... (thats the first place I would look for config files in a linux system) ... hell thats where you put your os config files (.bashrc, etc) ) ... most powerusers would look in the home directory imho ... if you want users to manually edit these files than you should document that they are in the users home directory ... – Joran Beasley Nov 03 '14 at 20:48
  • 2
    While some config files, e.g. "dot-files", are directly under the home directory, that is not a standard place. Check out the [Freedesktop specs](http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html), which many software packages do follow. Moreover, it's certainly not standard across platforms, for example on Windows. – Dun Peal Nov 03 '14 at 21:42