def which(cmd, mode=os.F_OK | os.X_OK, env=None):
<EOL>def _access_check(fn, mode):<EOL><INDENT>return (os.path.exists(fn) and os.access(fn, mode)<EOL>and not os.path.isdir(fn))<EOL><DEDENT>if _access_check(cmd, mode):<EOL><INDENT>return cmd<EOL><DEDENT>if env is None:<EOL><INDENT>env = os.environ<EOL><DEDENT>path = env.get("<STR_LIT>", os.defpath).split(os.pathsep)<EOL>if sys.platform == "<STR_LIT:win32>":<EOL><INDENT>if not os.curdir in path:<EOL><INDENT>path.insert(<NUM_LIT:0>, os.curdir)<EOL><DEDENT>default_pathext ='<STR_LIT>'<EOL>pathext = env.get("<STR_LIT>", default_pathext).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 not dir 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. `env` defaults to os.environ,
    if not supplied.