1

Possible Duplicate:
Test if executable exists in Python?

Is there a python function to let me detect whether a program is installed in the computer. I have a program that runs an .exe, that part works on windows but to run it in linux you need wine so I need a way for the python function to detect wine.

Community
  • 1
  • 1
wil
  • 171
  • 3

1 Answers1

1

You can use function os.get_exec_path() to get a list of directories which are set in PATH environment variable. If the executable you are looking for is not present in any of these directories it is correct to assume that the program is not installed.

The code snipped to determine whether there is Wine installed then would look like this:

import os
winePath = None
for directory in os.get_exec_path():
    testWinePath = os.path.join(directory, "wine")
    if os.path.exists(testWinePath) and os.access(testWinePath, os.R_OK | os.X_OK):
        winePath = executablePath
        break

If there is Wine installed then the path to its executable file (wine) will be in winePath variable; if not found winePath will be None. The code is also checking whether the file has correct perrmisions for reading and executing it.

The os.get_exec_path() is available since Python 3.2. In older versions you can use os.environ["PATH"].split(":") instead.

openhs
  • 41
  • 2