Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Created May 2, 2012 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SEJeff/2576984 to your computer and use it in GitHub Desktop.
Save SEJeff/2576984 to your computer and use it in GitHub Desktop.
Implementation of Unix's which command in python
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
@shadow-identity
Copy link

Why not shutil.which?

@adrianlzt
Copy link

Not available before python 3.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment