Retrieving the full path of Unix apps

Lorin Hochstein me at nospam.com
Tue Oct 5 21:55:04 CEST 2004


Hello all,

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 what I'm currently doing. Is there a more elegant way to do this?


def fullpath(prog):
     """Compute the full path of a program"""
     s = os.popen('which ' + prog).readline()[:-1]
     # Confirm that the return value looks like a path
     r = re.compile('/(\w+/)*' + prog)
     if r.match(s) is not None:
         return s
     else:
         raise ValueError,s



Thanks,

Lorin



More information about the Python-list mailing list