1from __future__ import annotations 2 3import os 4import sys 5 6 7def which(thefile: str) -> str | None: 8 path = os.environ.get("PATH", os.defpath).split(os.pathsep) 9 for d in path: 10 fname = os.path.join(d, thefile) 11 fnames = [fname] 12 if sys.platform == "win32": 13 exts = os.environ.get("PATHEXT", "").split(os.pathsep) 14 fnames += [fname + ext for ext in exts] 15 for name in fnames: 16 if os.access(name, os.F_OK | os.X_OK) and not os.path.isdir(name): 17 return name 18 return None 19