I don’t know if a module exists that acts like the which binary. If you don’t know what which does, I’ll tell you:
- given an argument ( which represents a binary/executable found on PATH ), which tells you the first location of that binary
Here’s the code I’m using:
import os
def which(program,additional_dirs=[]):
path = os.environ["PATH"]
path_components = path.split(":")
path_components.extend(additional_dirs)
for item in path_components:
location = os.path.join(item,program)
if os.path.exists(location):
return location
return None
The code is pretty straight-forward. The function takes a string as the program name, and an optional parameter that represents additional directories to look in. The default behaviour of this functions is to search the PATH variable.