prosseek
prosseek

Reputation: 191159

How can I find the path for an executable?

I need to setup environment with the path to a binary. In the shell, I can use which to find the path. Is there an equivalent in python? This is my code.

cmd = ["which","abc"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
res = p.stdout.readlines()
if len(res) == 0: return False
return True

Upvotes: 91

Views: 50831

Answers (7)

zonksoft
zonksoft

Reputation: 2429

There is distutils.spawn.find_executable().

Upvotes: 97

Anonymous
Anonymous

Reputation: 1

Here's a one-line version of earlier answers:

import os
which = lambda y: next(filter(lambda x: os.path.isfile(x) and os.access(x,os.X_OK),[x+os.path.sep+y for x in os.getenv("PATH").split(os.pathsep)]),None)

used like so:

>>> which("ls")
'/bin/ls'

Upvotes: -2

o9000
o9000

Reputation: 1676

This is the equivalent of the which command, which not only checks if the file exists, but also whether it is executable:

import os

def which(file_name):
    for path in os.environ["PATH"].split(os.pathsep):
        full_path = os.path.join(path, file_name)
        if os.path.exists(full_path) and os.access(full_path, os.X_OK):
            return full_path
    return None

Upvotes: 3

iLoveTux
iLoveTux

Reputation: 3625

I know this is an older question, but if you happen to be using Python 3.3+ you can use shutil.which(cmd). You can find the documentation here. It has the advantage of being in the standard library.

An example would be like so:

>>> import shutil
>>> shutil.which("bash")
'/usr/bin/bash'

Upvotes: 100

Gonzalo Larralde
Gonzalo Larralde

Reputation: 3541

There's not a command to do that, but you can iterate over environ["PATH"] and look if the file exists, which is actually what which does.

import os

def which(file):
    for path in os.environ["PATH"].split(os.pathsep):
        if os.path.exists(os.path.join(path, file)):
                return os.path.join(path, file)

    return None

Good luck!

Upvotes: 14

Greg Hewgill
Greg Hewgill

Reputation: 994599

If you use shell=True, then your command will be run through the system shell, which will automatically find the binary on the path:

p = subprocess.Popen("abc", stdout=subprocess.PIPE, shell=True)

Upvotes: 3

John Percival Hackworth
John Percival Hackworth

Reputation: 11531

You could try something like the following:

import os
import os.path
def which(filename):
    """docstring for which"""
    locations = os.environ.get("PATH").split(os.pathsep)
    candidates = []
    for location in locations:
        candidate = os.path.join(location, filename)
        if os.path.isfile(candidate):
            candidates.append(candidate)
    return candidates

Upvotes: 4

Related Questions