Skip to content

Instantly share code, notes, and snippets.

@Pithikos
Created May 18, 2015 11:00
Show Gist options
  • Save Pithikos/94f3078d87d8576138c1 to your computer and use it in GitHub Desktop.
Save Pithikos/94f3078d87d8576138c1 to your computer and use it in GitHub Desktop.
def find_cmd_abspath(cmd):
""" Returns the absolute path to a command.
None is returned if no absolute path was found.
"""
if exists(cmd) or exists(cmd + '.exe'):
return os.path.abspath(cmd)
if not 'PATH' in os.environ:
raise Exception("Can't find command path for current platform ('%s')" % sys.platform)
PATH = os.environ['PATH']
for path in PATH.split(os.path.pathsep):
for filename in os.listdir(path):
barename = os.path.splitext(filename)[0]
if filename == cmd or barename == cmd:
return path + os.path.sep + filename
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment