0

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:

  1. Direct Python code (rather than running a shell command)
  2. A Python 3-based solution, using pathlib.Path
  3. Platform-independent

Notes

  1. 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 Windows where command, or linux which command, but using Python code.
  2. 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.
LightCC
  • 9,804
  • 5
  • 52
  • 92
  • On Linux it's the environment variable `PATH`. – Barmar Apr 20 '22 at 22:18
  • https://docs.python.org/3/library/os.html#os.environ. – CristiFati Apr 20 '22 at 23:00
  • I'm not looking to get the PATH, but find files on it. Will update the Q. – LightCC Apr 21 '22 at 00:43
  • Does this answer your question? [How can I find the path for an executable?](https://stackoverflow.com/questions/5226958/how-can-i-find-the-path-for-an-executable) – safay Apr 21 '22 at 01:19
  • @safay That Q&A is focusing on executables, I'm looking for other patterns as well. It has good answers for exe's though (the `shutil.which` is good to know!). I'm also asking for pathlib. – LightCC Apr 21 '22 at 02:02

0 Answers0