How to obtain python file path

Ben Hutchings do-not-spam-ben.hutchings at businesswebsoftware.com
Wed Apr 2 08:58:18 EST 2003


In article <20030401165546102-0500 at news.covad.net>, Dan Grassi wrote:
> Not quite, that only provides the path to the initial .py file.
> 
> With a directory structure:
> fa
>     a.py
>     fb
>         b.py
> 
> cd .../fa
> execute a.py
> where a adds fb to sys.path
> a imports b.py
> 
> I need b.py to report it's path which would be .../fa/fb

Assuming the current directory hasn't changed since the file was
loaded, or the file was loaded with an absolute filename:

    def mydir():
        import os.path
        return os.path.abspath(os.path.dirname(__file__))




More information about the Python-list mailing list