Retrieving the full path of Unix apps

Nick Craig-Wood nick at craig-wood.com
Wed Oct 6 05:29:58 EDT 2004


Andrew Dalke <adalke at mindspring.com> wrote:
>  Here's an incomplete implementation of 'which'
> 
>  import os
> 
>  def is_executable(filename):
>        # Placeholder: not sure how to do this
>        return 1

def is_executable(filename):
    return os.access(filename, os.X_OK)

Is probably close enough unless you are running setuid (and even then
it probably does the right thing!)

>  def which(app):
>        dirnames = os.environ["PATH"].split(os.path.pathsep)
>        for dirname in dirnames:
>             filename = os.path.join(dirname, app)
>             if (os.path.exists(filename) and
>                 os.path.isfile(filename) and
>                 is_executable(filename)):
>                  return filename
>        return None

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list