Retrieving the full path of Unix apps

Grant Edwards grante at visi.com
Tue Oct 5 16:17:01 EDT 2004


On 2004-10-05, Lorin Hochstein <me at nospam.com> wrote:

> I'd like to retrieve the full path of an arbitrary program on
> a Unix system (e.g. gcc).

If you really have to worry about any arbitrary program, then
it's not really a solvable problem.  There may be any
non-negative number of paths for a file with a particular name.
(Including zero.) Finding an exhastive list requires you to
walk the entire directory tree from the root down.

> What's the nicest way to do this?

Do you care _which_ path you end up with in the case where
there are multiple ones that end in the filename of interest?

Are you assuming that the program is in one of the directories
listed in the PATH environment variable?

> 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?

You could grab the value of the environment variable PATH, and
search those directories for the file in question.  That's
pretty much what the 'which' command does.

-- 
Grant Edwards                   grante             Yow!  Is it FUN to be
                                  at               a MIDGET?
                               visi.com            



More information about the Python-list mailing list