How can I find a file the location of a file on the System PATH (i.e. in Windows %PATH% environment variable) with Python?
This is not about the Python Path, the built-in sys.path
, or the environment variable PYTHONPATH
, but the PATH from the main OS that is present in terminals such as CMD, bash, etc.
Preferences:
- Direct Python code (rather than running a shell command)
- A Python 3-based solution, using
pathlib.Path
- Platform-independent
Notes
- I'm not looking to just get the PATH (that is just
os.environ['PATH']
), but a solution to find a particular file on the PATH, similar to the Windowswhere
command, or linuxwhich
command, but using Python code. - I'm looking to find any file, not just executables (ideally you could put in a file pattern and get all results), different from this Q&A.