Finding the full path of an executable

Michael Hoffman 9qobl2n02 at sneakemail.com
Fri Jan 16 20:08:27 EST 2009


Unknown wrote:
> On 2009-01-16, Michael Hoffman <9qobl2n02 at sneakemail.com> 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:

Python 2.5.2 (r252:60911, Sep 23 2008, 19:04:15)
[GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> os.path.abspath("echo")
'/net/noble/vol2/home/mmh1/echo'
 >>> os.execvp(os.path.abspath("echo"), ["echo", "spam"])
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
   File 
"/net/noble/vol2/home/mmh1/arch/Linux-i386/opt/python-2.5.2/lib/python2.5/os.py", 
line 353, in execvp
     _execvpe(file, args)
   File 
"/net/noble/vol2/home/mmh1/arch/Linux-i386/opt/python-2.5.2/lib/python2.5/os.py", 
line 377, in _execvpe
     func(file, *argrest)
OSError: [Errno 2] No such file or directory
 >>> os.execvp("echo", ["echo", "spam"])
spam

The correct answer would be "/bin/echo" but abspath("echo") is just 
going to give me <cwd>/echo. 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.

Michael




More information about the Python-list mailing list