find_executable

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

Finds 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.

  • normalized (bool) – Whether to normalize the returned pathname; defaults to True.

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
>>> import sys
>>> python_exe = "python.exe"
>>> python_exe_exists, path_to_python_exe = find_executable(python_exe)
>>> python_exe_exists
True
>>> possible_paths = [os.path.dirname(sys.executable), sys.executable]
>>> target = possible_paths[0]
>>> python_exe_exists, path_to_python_exe = find_executable(python_exe, target=target)
>>> python_exe_exists
False
>>> target = possible_paths[1]
>>> python_exe_exists, path_to_python_exe = find_executable(python_exe, target=target)
>>> python_exe_exists
True
>>> python_exe_exists, path_to_python_exe = find_executable(possible_paths[1])
>>> python_exe_exists
True
>>> 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'