find_executable

pyhelpers.dirs.find_executable(name, options=None, target=None)[source]

Get the pathname of an executable file for a specified application.

Parameters:
  • name (str) – Name or filename of the application that is to be called.

  • options (list | set | None) – Possible pathnames or directories to search for the executable; defaults to None.

  • target (str | None) – Specific pathname of the executable file (if already known); defaults to None.

Returns:

Whether the specified executable file exists (i.e. a boolean indicating existence), together with its pathname.

Return type:

tuple[bool, str]

Examples:

>>> from pyhelpers.dirs import find_executable
>>> import os
>>> python_exe = "python.exe"
>>> possible_paths = ["C:\Program Files\Python39", "C:\Python39\python.exe"]
>>> python_exe_exists, path_to_python_exe = find_executable(python_exe, possible_paths)
>>> python_exe_exists
True
>>> os.path.relpath(path_to_python_exe)
'venv\Scripts\python.exe'
>>> text_exe = "pyhelpers.exe"  # This file does not actually exist
>>> test_exe_exists, path_to_test_exe = find_executable(text_exe, possible_paths)
>>> test_exe_exists
False
>>> os.path.relpath(path_to_test_exe)
'pyhelpers.exe'