Finding the full path of an executable
Grant Edwards
grante at visi.com
Fri Jan 16 20:25:49 EST 2009
On 2009-01-17, Michael Hoffman <9qobl2n02 at sneakemail.com> wrote:
> 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:
[...]
> 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()
'/bin/echo'
If you want to avoid using the external "which", here's a recipe:
http://code.activestate.com/recipes/52224/
--
More information about the Python-list
mailing list