Obtaining hte currently running script under windows

Ryan Ginstrom software at ginstrom.com
Fri Jun 8 21:27:02 EDT 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
frozen.

Regards,
Ryan

--
Ryan Ginstrom
translation at ginstrom.com
http://ginstrom.com/  




More information about the Python-list mailing list