def which(cmd, mode=os.F_OK | os.X_OK, path=None):
<EOL>def _access_check(fn, mode):<EOL><INDENT>return (os.path.exists(fn) and os.access(fn, mode) and<EOL>not os.path.isdir(fn))<EOL><DEDENT>if _access_check(cmd, mode):<EOL><INDENT>return cmd<EOL><DEDENT>path = (path or os.environ.get("<STR_LIT>", os.defpath)).split(os.pathsep)<EOL>if sys.platform == "<STR_LIT:win32>":<EOL><INDENT>if os.curdir not in path:<EOL><INDENT>path.insert(<NUM_LIT:0>, os.curdir)<EOL><DEDENT>pathext = os.environ.get("<STR_LIT>", "<STR_LIT>").split(os.pathsep)<EOL>matches = [cmd for ext in pathext if cmd.lower().endswith(ext.lower())]<EOL>files = [cmd] if matches else [cmd + ext.lower() for ext in pathext]<EOL><DEDENT>else:<EOL><INDENT>files = [cmd]<EOL><DEDENT>seen = set()<EOL>for dir in path:<EOL><INDENT>dir = os.path.normcase(dir)<EOL>if dir not in seen:<EOL><INDENT>seen.add(dir)<EOL>for thefile in files:<EOL><INDENT>name = os.path.join(dir, thefile)<EOL>if _access_check(name, mode):<EOL><INDENT>return name<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return None<EOL>
Given a command, mode, and a PATH string, return the path which
    conforms to the given mode on the PATH, or None if there is no such
    file.
    `mode` defaults to os.F_OK | os.X_OK. `path` defaults to the result
    of os.environ.get("PATH"), or can be overridden with a custom search
    path.