Retrieving the full path of Unix apps
Andrew Dalke
adalke at mindspring.com
Tue Oct 5 17:09:48 EDT 2004
Lorin Hochstein wrote:
> I'd like to retrieve the full path of an arbitrary program on a Unix
> system (e.g. gcc). What's the nicest way to do this? Currently I'm
> invoking the "which" program and parsing what it outputs to determine if
> the output looks like a path.
Here's an incomplete implementation of 'which'
import os
def is_executable(filename):
# Placeholder: not sure how to do this
return 1
def which(app):
dirnames = os.environ["PATH"].split(os.path.pathsep)
for dirname in dirnames:
filename = os.path.join(dirname, app)
if (os.path.exists(filename) and
os.path.isfile(filename) and
is_executable(filename)):
return filename
return None
>>> print which("ls")
/bin/ls
>>> print which("qwerty")
None
>>> print which("python")
/usr/local/bin/python
>>>
Andrew
dalke at dalkescientific.com
More information about the Python-list
mailing list