How to find script's directory

Jeffrey Froman jeffrey at fro.man
Sun Aug 19 12:48:28 EDT 2007


Papalagi Pakeha wrote:

> I guess I could do it with a little help of os.path.realpath() for all
> those cases where absolute or relative path was used. But how should I
> approach it when it's invoked as plain 'blah.py' because its directory
> name is in $PATH?


Use the value of __file__ rather than sys.argv[0] -- it contains the
directory information. You may still need to normalize the value using
various os.path functions.


Jeffrey



More information about the Python-list mailing list