Behaviour of os.path.join

Eryk Sun eryksun at gmail.com
Thu May 28 09:51:15 EDT 2020


On 5/27/20, Chris Angelico <rosuav at gmail.com> wrote:
> On Thu, May 28, 2020 at 7:07 AM BlindAnagram <blindanagram at nowhere.com>
> wrote:
>> You can define a path however you want but it won't change the fact that
>> on Windows a path that ends in '\\' is inherently a path to a directory.
>
> Citation needed.

See [MS-FSA] 2.1.5.1 Server Requests an Open of a File [1]. Here are
the relevant statements:

    The operation MUST be failed with STATUS_OBJECT_NAME_INVALID under
    any of the following conditions:
    ....
      * If PathName contains a trailing backslash and
        CreateOptions.FILE_NON_DIRECTORY_FILE is TRUE.

    ....
    If PathName contains a trailing backslash:
      * If StreamTypeToOpen is DataStream or
        CreateOptions.FILE_NON_DIRECTORY_FILE is TRUE, the operation MUST
        be failed with STATUS_OBJECT_NAME_INVALID

Internally, WinAPI CreateFileW calls NTAPI NtCreateFile with the
create option FILE_NON_DIRECTORY_FILE (i.e. only open or create a data
file stream), unless backup semantics are requested in order to be
able to open a directory (i.e. an index stream), in which case the
call uses neither FILE_NON_DIRECTORY_FILE nor FILE_DIRECTORY_FILE and
leaves it up to the path name. In other words, with backup semantics,
if you need to ensure that only a directory is opened, add a trailing
slash.

NTAPI STATUS_OBJECT_NAME_INVALID translates to WinAPI
ERROR_INVALID_NAME (123), which translates to C EINVAL (22).

[1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fsa/8ada5fbe-db4e-49fd-aef6-20d54b748e40


More information about the Python-list mailing list