Created
May 2, 2012 14:35
-
-
Save SEJeff/2576984 to your computer and use it in GitHub Desktop.
Implementation of Unix's which command in python
This file contains 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(exe=None): | |
''' | |
Python clone of POSIX's /usr/bin/which | |
''' | |
if exe: | |
(path, name) = os.path.split(exe) | |
if os.access(exe, os.X_OK): | |
return exe | |
for path in os.environ.get('PATH').split(os.pathsep): | |
full_path = os.path.join(path, exe) | |
if os.access(full_path, os.X_OK): | |
return full_path | |
return None |
Not available before python 3.3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why not
shutil.which
?