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.