Hello python devs.
One thing which appears to be pretty common in many scripts is figuring out module's parent directory path. 
I often find myself writing:

HERE = os.path.abspath(os.path.dirname(__file__))

...at the top of a module. 
Also, it is not rare to do further concatenations in order to declare where static files are located:

CONF_FILE = os.path.abspath(os.path.join(HERE, 'app.conf'))
CERT_FILE = os.path.abspath(os.path.join(HERE, 'cert.pem'))
THIS_FILE = os.path.abspath(os.path.join(HERE, __file__))

A quick search within Python source code shows this is indeed a very common pattern:

$ find . -name \*.py | xargs grep "os\.path" | grep __file__

Just some examples (there are *many*):

http://hg.python.org/cpython/file/186f6f56f4bc/Lib/distutils/tests/__init__.py#l21
http://hg.python.org/cpython/file/186f6f56f4bc/PCbuild/build_tkinter.py#l11
http://hg.python.org/cpython/file/186f6f56f4bc/PCbuild/build_ssl.py#l68
http://hg.python.org/cpython/file/186f6f56f4bc/Tools/msi/uisample.py#l2
http://hg.python.org/cpython/file/186f6f56f4bc/Lib/distutils/tests/test_config_cmd.py#l30
http://hg.python.org/cpython/file/186f6f56f4bc/Lib/unittest/test/testmock/__main__.py#l7
http://hg.python.org/cpython/file/186f6f56f4bc/Lib/pydoc.py#l2463

I think there's space for improvements here so my proposal is to add a simple os.path.here() function working like this:

>>> # assuming /home/giampaolo/app/run.py is the current module:
>>> os.path.here()
/home/giampaolo/app/
>>> os.path.here(__file__)
/home/giampaolo/app/run.py
>>> os.path.here('..')
/home/giampaolo/

The implementation is pretty straightforward:

def here(concat=None):
    """Return the absolute path of the parent directory where the 
    script is defined.
    """
    here = os.path.abspath(os.path.dirname(__file__))
    if concat is not None:
        here = os.path.abspath(os.path.join(here, concat))
    return here

Thoughts?


--
Giampaolo - http://grodola.blogspot.com