This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: [win] shutil.which can not find the path if 'cmd' include directory path and not include extension name
Type: behavior Stage: resolved
Components: Library (Lib), Windows Versions: Python 3.8, Python 3.7, Python 3.6
Status: closed Resolution: duplicate
Dependencies: Superseder: shutil.which wrong result on Windows
View: 24505
Assigned To: Nosy List: SpecLad, eryksun, paul.moore, seahoh, serhiy.storchaka, steve.dower, tim.golden, ys19991, zach.ware
Priority: normal Keywords:

Created on 2019-08-20 15:59 by seahoh, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg350019 - (view) Author: Wator Sead (seahoh) Date: 2019-08-20 15:59
The current code is:
    if os.path.dirname(cmd):
        if _access_check(cmd, mode):
            return cmd
        return None

In Windows, if 'cmd' include directory path and not include extension name, it return 'None'.
e.g. a file's path is 'd:\dir\app.exe', call shutil.which with 'cmd=="d:\dir\app"'.

How about this patch:
    if os.path.dirname(cmd):
        path, cmd = os.path.split(cmd)
msg350154 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2019-08-22 04:41
The code that sets up the PATHEXT `files` could be moved up. It also needs to be fixed in order to implement the correct behavior. For example:

    use_bytes = isinstance(cmd, bytes)

    files = [cmd]
    if _WINDOWS:
        # Also look for the name plus each PATHEXT extension.
        default_pathext = '.COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC'
        pathext = os.environ.get('PATHEXT', default_pathext)
        if use_bytes:
            pathext = os.fsencode(pathext).split(b';')
            pathext = pathext.split(';')
        for ext in pathext:
            files.append(cmd + ext)

    # If we're given a path with a directory part, look it up directly rather
    # than referring to PATH directories. This includes checking relative to the
    # current directory, e.g. ./script.
    if os.path.dirname(cmd):
        for file in files:
            if _access_check(file, mode):
                return file
        return None

The author of the PATHEXT code in shutil.which() was following a source that documented the behavior incorrectly. CMD always looks for the filename before trying to append the PATHEXT extensions. It does not limit its search to PATHEXT extensions. The only exception is a file that has no extension, in which case "." has to be set in PATHEXT to get CMD to find the file. However, where.exe finds a file that has no extension, regardless of PATHEXT, so Python's which() should be free to follow that example.
msg350168 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-08-22 07:16
Do you mind to create a PR Eryk?
msg373438 - (view) Author: Wansoo Kim (ys19991) * Date: 2020-07-10 03:25
Can I solve this problem?
Date User Action Args
2022-04-11 14:59:19adminsetgithub: 82075
2021-03-20 02:13:46eryksunsetstatus: open -> closed
superseder: shutil.which wrong result on Windows
resolution: duplicate
stage: needs patch -> resolved
2020-07-10 03:25:42ys19991setnosy: + ys19991
messages: + msg373438
2019-08-27 20:40:43SpecLadsetnosy: + SpecLad
2019-08-22 07:16:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg350168
2019-08-22 04:41:10eryksunsetversions: - Python 3.5
nosy: + eryksun

messages: + msg350154

stage: needs patch
2019-08-20 16:36:14xtreaksetnosy: + paul.moore, tim.golden, zach.ware, steve.dower
components: + Windows
2019-08-20 15:59:37seahohcreate