Finding location of an executable in a windows machine?

Dave Angel davea at
Fri May 15 14:01:18 EDT 2009

nonsense at wrote:
> My python script calls another windows program file. I have the path
> to that program hardcoded in it. Is there a way for python to
> automatically find the path to this program?
> testapp_path = "C:\\Program Files\\testapp\\version\\61\\"
> Or do i have to do a brute force approach to searching all drives/
> folders till it finds a path matching my program name?
Do you actually need the path, or do you just want the program to run as 
though you had entered it at a command prompt?  Many of the APIs for 
launching other programs will do a path search, similar to what the DOS 
box in Windows does.  Try just leaving off the path entirely.

Or you can get the PATH environment variable, parse it, and search for 
those locations yourself.  But realize that the shell also looks in a 
few places not on the path, including at least  current directory and 
windows directory (eg.  c:\windows\system32 ).  And you very likely want 
to look a few other places, such as in Program Files.

But searching the drives raw is rarely a good idea, as it's quite likely 
there's a duplicate filename out there somewhere, and you want a 
deterministic algorithm for which one will run.

Depending on how fancy your program is, you may want to do the searching 
at install time, and store the result in an xxx.ini file or equivalent.  
That file would probably go somewhere in "Documents and Settings" 
directory structure, perhaps based on the current user.  That way you 
can document how the user will customize it.

If you need help on a specific python command, you'd also need to tell 
us what version of Python you're using.
     print sys.version

More information about the Python-list mailing list