path to data files

Daniel Fetchinson fetchinson at googlemail.com
Thu Aug 19 06:25:17 EDT 2010


If a python module requires a data file to run how would I reference
this data file in the source in a way that does not depend on whether
the module is installed system-wide, installed in $HOME/.local or is
just placed in a directory from where the interpreter is fired up? I'd
like to always keep the python source and the data file in the same
directory, be it /usr/lib/python2.6/site-packages,
$HOME/.local/lib/python2.6/site-packages or
/arbitrary/path/to/somewhere.

If the data file is called 'foo' and I simply do open('foo') in the
python source file this will not work if they are both installed
either system-wide or locally. More precisely if the module is called
'foo.py', data file called 'foo', they are both in
/usr/lib/python2.6/site-packages and if foo.py I have open('foo') I'll
get a file not found error.

Any ideas?

Cheers,
Daniel



-- 
Psss, psss, put it down! - http://www.cafepress.com/putitdown



More information about the Python-list mailing list