__file__ is sometimes absolute, sometimes relative

Discussion in 'Python' started by Sébastien Barthélemy, Oct 1, 2010.

  1. Hello,

    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

    The first option does not work, because __path__ is initialised after the
    module is loaded (apparently).

    Solution 2 works, but behaves differently when ran from python or
    from a doctest file. The path in __file__ is absolute if the program
    is ran directly by ``python``, and relative if it is ran by
    ``python -m doctest``.

    Solution 3 works. However if there is a better way to do this, please let me
    know.

    I could not find anything documenting what the proper value for __file__
    should be. Maybe my problem with solution 2 is a bug in doctest or runpy?

    I put up a simple example exhibiting the problem.

    Here are the results on my mac (using python from macport)
    I get the same results using python 2.6, 2.7 and 3.1

    $ python2.6 test.py
    /Users/seb/Devel/arboris-python/src/pyfile
    /Users/seb/Devel/arboris-python/src/pyfile/mod.pyc
    /Users/seb/Devel/arboris-python/src/pyfile
    /Users/seb/Devel/arboris-python/src/pyfile

    $ python2.6 -m doctest test.txt
    **********************************************************************
    File "test.txt", line 5, in test.txt
    Failed example:
    print mod.__file__
    Expected:
    /Users/seb/Devel/arboris-python/src/pyfile/mod.pyc
    Got:
    mod.pyc
    **********************************************************************
    File "test.txt", line 7, in test.txt
    Failed example:
    print(mod.whereami)
    Expected:
    /Users/seb/Devel/arboris-python/src/pyfile
    Got:
    <BLANKLINE>
    **********************************************************************
    File "test.txt", line 9, in test.txt
    Failed example:
    print(mod.whereami2)
    Expected:
    /Users/seb/Devel/arboris-python/src/pyfile
    Got:
    /Users/seb/Devel/arboris-python/src/pyfile/
    **********************************************************************
    1 items had failures:
    3 of 6 in test.txt
    ***Test Failed*** 3 failures.

    Cheers
    Sebastian
     
    Sébastien Barthélemy, Oct 1, 2010
    #1
    1. Advertising

  2. Sébastien Barthélemy <> 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
     
    Arnaud Delobelle, Oct 1, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. klenwell

    __file__ vs __FILE__

    klenwell, Nov 3, 2007, in forum: Python
    Replies:
    11
    Views:
    953
    Matimus
    Nov 5, 2007
  2. Stef Mientki
    Replies:
    0
    Views:
    228
    Stef Mientki
    Oct 18, 2008
  3. Keith Thompson

    Re: __FILE__: filename from absolute pathname?

    Keith Thompson, May 7, 2011, in forum: C Programming
    Replies:
    4
    Views:
    872
    Harald van Dijk
    May 8, 2011
  4. Simon Strandgaard

    [rcr] absolute __FILE__

    Simon Strandgaard, May 1, 2004, in forum: Ruby
    Replies:
    1
    Views:
    132
    David Alan Black
    May 1, 2004
  5. James Byrne
    Replies:
    3
    Views:
    600
    James Byrne
    Sep 14, 2010
Loading...

Share This Page