Created
January 5, 2017 18:40
-
-
Save kodelint/196de65092f121d06f3b17c07738ad80 to your computer and use it in GitHub Desktop.
nix like which function for python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def which(program): | |
""" | |
:param program: i.e. docker, python etc | |
:return: full path for the given binary | |
""" | |
import os | |
def is_exe(fpath): | |
return os.path.isfile(fpath) and os.access(fpath, os.X_OK) | |
fpath, fname = os.path.split(program) | |
if fpath: | |
if is_exe(program): | |
return program | |
else: | |
for path in os.environ["PATH"].split(os.pathsep): | |
path = path.strip('"') | |
exe_file = os.path.join(path, program) | |
if is_exe(exe_file): | |
return exe_file | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment