Accessing application data portably
Larry Bates
larry.bates at websafe.com
Thu Aug 24 09:46:11 EDT 2006
Tom E H wrote:
> Larry Bates wrote:
>>> Well that's great, but how do you access the ini file portably?
>> From my original post:
>>
>> Then I use ConfigParser in my application...
>
> Thanks, but where in the directory structure do you put the ini file on
> different platforms? Presumably you have to hard-code that into the source
> and then do operating system type detection?
>
> i.e. if I make my config parser:
>
> import ConfigParser
> config = ConfigParser.ConfigParser()
> config.read(filename)
>
> What do you use for filename on Windows? What on Linux? OSX? etc. How do
> you detect which operating system you are running on?
>
> Tom
>
I almost always have the .ini configuration file live in the same
directory/folder as the program or sometimes in a subdirectory of
the install directory (which I reference relative to where the
program is run from). Typically I install a default .ini file
via program installer (I like Inno Installer on Windows). I also
make all my programs accept a -i <configuration file path> argument
when they are run so you can override the default .ini file on the
command line.
example:
myprog -i C:\aaa\bbb\myprog.ini
As a default I do config.read('myprog.ini') it always reads from
the current directory (which is where the program is installed).
To access a subdirectory of the current directory I do something
like:
p=os.path.join(os.getcwd(), 'configfiles')
config.read(p)
I haven't put anything on OSX but this works fine on Windows
and Linux and should work on OSX.
-Larry Bates
More information about the Python-list
mailing list