moisesgallego
moisesgallego

Reputation: 421

Know the config folder path in Python

I am creating a small application in Python that needs to read a configuration file.

I intend to save this file in a folder in /etc on Linux, but I would like my application to run on other operating systems.

The question is: Is there some kind of constant, variable, package, etc. that can tell me the path to the most sensible settings folder on an OS?

Upvotes: 0

Views: 3627

Answers (3)

Ekaterina Mishina
Ekaterina Mishina

Reputation: 1723

Maybe it is better to locate your configuration file(s) in a folder related to your actual module/application? Determine the location of your module might not be straight forward, but this thread should help.

One tool which maybe useful, configparser, has a function which actually looks for configuration files in some standard locations: RawConfigParser.read(filenames)

Upvotes: 3

BrtH
BrtH

Reputation: 2664

As far as I know there is no builtin constant for this, but it seems fairly easy to do it manually.
You could use sys.platform or os.name to get the OS, and then set the config folder according to this.

os_name = sys.platform
cfg_folder = {'linux2': '/etc', 'win32': 'folder', ...}[os_name]

Upvotes: 2

Edward Coelho
Edward Coelho

Reputation: 235

You might want to check http://docs.python.org/library/os.html#os-file-dir . After getting the root path you are able to select specific paths depending on the working OS.

Upvotes: 1

Related Questions