finding out absolute path to a file from python

filesystems, io, python

Solution

the answer is to use:

 __file__

which returns a relative path.

os.path.abspath(__file__) 

can be used to get the full path.

Problem

If I have a file `test.py` that resides in some directory, how can I find out from `test.py` what directory it is in? `os.path.curdir` will give the current directory but not the directory where the file lives. If I invoke `test.py` from some directory `foo`, `os.curdir` will return `foo` but not the path of `test.py`. thanks.

Original source