views:

56

answers:

1

Simple case

I have a Python program that I intend to support on both *nix and Windows systems. The program must be configurable, at least globally. Is there a cross-platform way to address the configuration file?

I.e. I want to write instead of

import platform
if platform.system() == "Windows":
    configFilePath = "C:\MyProgram\mainconfig.ini"
else:
    configFilePath = "/etc/myprogram/mainconfig.ini"

something along the lines of

import configmagic
configFile = configmagic("myprogram", "mainconfig")


A slightly more advanced case

Can the same be applied to user-specific configuration? I.e. to keep the configuration in ~user/.myprogram/ under Unix, and in HKEY_LOCAL_USER registry section under Windows?

+1  A: 

Python will allow forward-slash paths on Windows, and os.path.expanduser works on Windows also, so you can get a user-specific file path using:

config_file = os.path.expanduser("~/foo.ini")

if you want to find a .ini in the user's home directory. I'm not sure how to unify file-based .ini and registry settings.

Ned Batchelder
Great, and is there a way to solve the first case (global configuration file path)?
David Parunakian