Obtaining hte currently running script under windows

Ryan Ginstrom software at ginstrom.com
Sat Jun 9 03:27:02 CEST 2007

> On Behalf Of Sean Farrow
> Is there any way to obtain the full path to the currently 
> running script under win32?
> I am using the pythonw.exe file is that helps.
> Sean.

Current working directory is not always reliable. There is a __file__
variable set if python.exe runs your script.

import os
import sys

module_name = os.path.dirname(unicode(__file__, sys.getfilesystemencoding(

If your script is frozen via py2exe, you can use the sys.executable value to
get the module directory.

import os
import sys

module_name = os.path.dirname(unicode(sys.executable,
sys.getfilesystemencoding( )))

(You need the Unicode stuff if your path may include non-ASCII characters)

function module_path() in the alternate solution at
http://www.py2exe.org/index.cgi/WhereAmI will give you the module
(script/executable) directory whether your script is running normally or


Ryan Ginstrom
translation at ginstrom.com

More information about the Python-list mailing list