How do I find executable files from the environment PATH variable?
noah at noah.org
Tue Aug 6 03:02:20 CEST 2002
Basically I want to write "which" in Python.
Is there a way I can find if a given executable exists
in the environment PATH?
os.path does not have any functions to help. None of them expand the
PATH environment variable. Also os.access() requires an absolute path.
Am I forced to check every directory in PATH
and test each one with os.access()? If so then
will this script duplicate the actions of the
shell in searching for an executable file?
import os, sys
def which (filename):
if not os.environ.has_key('PATH') or os.environ['PATH'] == '':
p = os.defpath
p = os.environ['PATH']
pathlist = p.split (os.pathsep)
for path in pathlist:
f = os.path.join(path, filename)
if os.access(f, os.X_OK):
print which (sys.argv)
Thanks for any help!
More information about the Python-list