Finding the full path of an executable

Grant Edwards grante at
Sat Jan 17 02:25:49 CET 2009

On 2009-01-17, Michael Hoffman <9qobl2n02 at> wrote:
> Unknown wrote:
>> On 2009-01-16, Michael Hoffman <9qobl2n02 at> wrote:
>>> Is there a portable way to find the full path of a filename that would 
>>> be called by os.execvp()?
>> Yes.  Use os.path.abspath() on the name before you call it with
>> os.execvp()
> That doesn't work:


> The correct answer would be "/bin/echo" but abspath("echo") is just 
> going to give me <cwd>/echo.

My mistake.  I thought you had a relative path to the program.

> I need something that will search through the PATH like
> execvp() would. I can do it myself, but I'm surprised that 
> such a feature is not already readily available somewhere.

Since there are system calls that search the PATH, applications
don't generally have to do it.  There is a pretty standard Unix
utility called "which" that does it:

>>> os.popen("which %s" % "echo").read().strip()

If you want to avoid using the external "which", here's a recipe:


More information about the Python-list mailing list