Finding the full path of an executable

M

Michael Hoffman

Is there a portable way to find the full path of a filename that would
be called by os.execvp()?

Thanks,
Michael Hoffman
 
M

Michael Hoffman

Unknown said:
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
 
G

Gabriel Genellina

En Fri, 16 Jan 2009 23:08:27 -0200, escribiste en el grupo
gmane.comp.python.general
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.

py> import distutils.spawn
py> help(distutils.spawn.find_executable)
Help on function find_executable in module distutils.spawn:

find_executable(executable, path=None)
Try to find 'executable' in the directories listed in 'path' (a
string listing directories separated by 'os.pathsep'; defaults to
os.environ['PATH']). Returns the complete filename or None if not
found.

py> distutils.spawn.find_executable("sc")
'C:\\WINDOWS\\system32\\sc.exe'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top