Finding location of an executable in a windows machine?

N

nonsense

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?
 
M

MRAB

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?

The path to the Python script is given by __file__. If your other
program is in the same folder then you can use:

other_app_path = os.path.join(os.path.dirname(__file__),
other_app_name)
 
D

Dave Angel

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
 
T

Terry Reedy

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?

Program Files directories are usually not part of the PATH environment
variable. A main program in Program Files is generally started by
clicking on: an icon that points to it, an entry in Start Menu, its
directory listing in Explorer, or a file whose extension is associated
with it. A subsidiary program is started by a main program that knows
where is it in relation to the main program.

If a program is registered when installed, then there will be a registry
key that is the same for all installations that points to its
installation-specific location. Python on Windows comes with a winreg
module for accessing the registry. You just need to find the key,
possibly using the regedit utility and its search function.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top