Accessing application data portably

Larry Bates larry.bates at websafe.com
Wed Aug 23 16:47:29 EDT 2006


Tom E H wrote:
> My Python application includes some data files that need to be accessed by
> modules I distribute with it.
> 
> Where can I put them, and how should I arrange my code, so that it works
> across platforms?
> 
> On Linux, I could install the data to "/usr/lib/myprogram/datafile", and
> on Windows to "datafile" relative to where the executable (made by
> py2exe) is installed.  Then I could detect the operating system, and choose
> appropriately.
> 
> To be that explicit seems undesirable.  Any cleverer ideas?
> 
> Tom
> 
> (Please CC me on replies: I'm not subscribed.  The From address is munged)
> 
I almost always send along an application.ini file and put the location
of where my data is to be stored in that file instead of imbedding (or
worse, hard-coding) it in the application program itself.  I also put
other parameters that the user might want to change that will change
the behavior of my program (debugging, logging, etc.) there also.  Then
during installation I modify the option in this file with the install
script.

Something like:

[init]
debug=0
quiet=0
datafilepath=/usr/lib/myprogram/datafile

or

[init]
debug=0
quiet=0
datafilepath=C:\Program Files\myprogram\datafile

Then I use ConfigParser in my application to read this file and
extract the parameters.  Makes it easy for more experienced users
(and me) to be able to easily relocate the datafile if they
desire.

On Windows I use Inno Installer and it can modify these options inside the
.ini file during the installation so that datafilepath points to where
my data actually will live.  Works perfectly for me.

-Larry Bates



More information about the Python-list mailing list