how to organize a module that requires a data file

Terry Hancock hancock at anansispaceworks.com
Thu Nov 17 14:52:21 EST 2005


On Thu, 17 Nov 2005 12:18:51 -0700
Steven Bethard <steven.bethard at gmail.com> wrote:
> My problem is with the text file.  Where should I keep it?
> 
> I can only think of a few obvious places where I could
> find the text  file at import time -- in the same
> directory as the module (e.g.  lib/site-packages), in the
> user's home directory, or in a directory  indicated by an
> environment variable.  

Why don't you search those places in order for it?

Check ~/.mymod/myfile, then /etc/mymod/myfile, then
/lib/site-packages/mymod/myfile or whatever. It won't take
long, just do the existence checks on import of the module.
If you don't find it after checking those places, *then*
raise an exception.

You don't say what this data file is or whether it is
subject to change or customization. If it is, then there is
a real justification for this approach, because an
individual user might want to shadow the system install with
his own version of the data.

That's pretty typical behavior for configuration files on
any Posix system.

Cheers,
Terry


-- 
Terry Hancock (hancock at AnansiSpaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com




More information about the Python-list mailing list