__file__ is sometimes absolute, sometimes relative

Arnaud Delobelle arnodel at gmail.com
Fri Oct 1 18:43:21 CEST 2010


Sébastien Barthélemy <barthelemy at crans.org> writes:

> Hello,

Hi!

> I use a data file that lies on disk in the same directory that the
> module which makes use of it.
>
> The data file is checked in the repository and gets installed by
> the distutils ``package_data`` directive, so it is in place both
> during development and after and install.
>
> In my program, I need to find the absolute path to this data file.
>
> I could think of 3 ways to do this:
> 1. using the __path__ property in the module
> 2. using __file__ property in the module
> 3. using __file__ and os.getcwd() in the module

I have used the following, but I don't know either if it is a good way:

    os.path.dirname(os.path.realpath(__file__))

-- 
Arnaud




More information about the Python-list mailing list