get directory name of file being executed

Tim Evans t.evans at paradise.net.nz
Thu Feb 27 04:02:14 EST 2003


davidw at dedasys.com (David N. Welton) writes:

> In Tcl, you do this with 'info script'.
> 
> I'm doing this:
> 
> /usr/bin/python2 /some/script/in/a/dir/foo.py
> 
> I want to get "/some/script/in/a/dir/", not the cwd of where I
> launched the script from.
> 
> Is that possible?
> 
> Thankyou,

------------------------------------------------------
import sys, os.path

dir = os.path.abspath(os.path.dirname(sys.argv[0]))
------------------------------------------------------

The use of os.path.abspath is to ensure that

  cd /some/script/in/a/dir ; python2.2 foo.py

will give the same result.

-- 
Tim Evans




More information about the Python-list mailing list