Finding the full path of an executable

Discussion in 'Python' started by Michael Hoffman, Jan 16, 2009.

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

    Thanks,
    Michael Hoffman
     
    Michael Hoffman, Jan 16, 2009
    #1
    1. Advertising

  2. Unknown wrote:
    > On 2009-01-16, Michael Hoffman <> 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
     
    Michael Hoffman, Jan 17, 2009
    #2
    1. Advertising

  3. En Fri, 16 Jan 2009 23:08:27 -0200, escribiste en el grupo
    gmane.comp.python.general
    > Unknown wrote:
    >> On 2009-01-16, Michael Hoffman <> wrote:
    >>> Is there a portable way to find the full path of a filename that would
    >>> be called by os.execvp()?


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

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 17, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Josephine Schafer
    Replies:
    1
    Views:
    3,608
    Philip Morley
    Jul 21, 2003
  2. Kaiser S.
    Replies:
    3
    Views:
    986
  3. Replies:
    7
    Views:
    466
  4. koranthala

    executable path finding

    koranthala, Aug 31, 2009, in forum: Python
    Replies:
    3
    Views:
    350
    ryles
    Sep 1, 2009
  5. dude
    Replies:
    5
    Views:
    1,720
    Jussi Piitulainen
    Mar 17, 2011
Loading...

Share This Page