code: def which(filename, interactive=False, verbose=False):
      
      exe = [e.lower() for e in os.environ.get('PATHEXT', '').split(';')]
      if sys.platform != 'win32':  # pragma: nocover
          exe.append('')
  
      name, ext = os.path.splitext(filename)
      has_extension = bool(ext)
      if has_extension and ext.lower() not in exe:
          raise ValueError("which can only search for executable files")
  
      def match(filenames):
          """Returns the sorted subset of ``filenames`` that matches ``filename``.
          """
          res = set()
          for fname in filenames:
              if fname == filename:  # pragma: nocover
                  res.add(fname)  # exact match
                  continue
              fname_name, fname_ext = os.path.splitext(fname)
              if fname_name == name and fname_ext.lower() in exe: # pragma: nocover
                  res.add(fname)
          return sorted(res)
  
      returnset = set()
      found = False
      for pth in get_path_directories():
          if verbose:  # pragma: nocover
              print('checking pth..')
  
          fnames = _listdir(pth, exe)
          if not fnames:
              continue
  
          for m in match(fnames):
              found_file = _normalize(os.path.join(pth, m))
              if found_file not in returnset:  # pragma: nocover
                  if is_executable(found_file):
                      yield found_file
                  returnset.add(found_file)
          found = True
  
      if not found and interactive:  # pragma: nocover
          print("Couldn't find %r anywhere on the path.." % filename)
          sys.exit(1)
docs: Yield all executable files on path that matches `filename`.