Skip to content

ntpath not adding slash after drive letters #124167

Closed as not planned
Closed as not planned

Description

@matan1008

Bug report

Bug description:

import ntpath
print(ntpath.join('c:', 'x', 'y'))

I would expect the following code to print 'c:\\x\\y', yet it prints 'c:x\\y', Would you expect a PR that changes the current behaviour?

CPython versions tested on:

3.10, 3.11, 3.12, 3.13, CPython main branch

Operating systems tested on:

Linux, Windows

Activity

  1. Wulian233 commented on Sep 17, 2024

    @Wulian233
    Contributor

    In my previous PR
    #122372
    I found that ntpath and nturl2path are not documented. In the comment of nturl2path, it is mentioned that it is only used by urllib, but it is not in the urllib folder and is not a private module with an _ prefix (like _colorize), so we can import and use it freely.

    https://github.com/python/cpython/blob/main/Lib/nturl2path.py

    Searching for "import nturl2path" on GitHub also shows that a considerable number of people use it. I am curious why these two modules do not have documentation, is it a historical legacy issue?

    Discussion #120423

    They are not restricted by the system, but the functions they provide are Windows-specific file paths. Should we add their documentation? Or keep it that way

    I want to answer this question, but I don't know if it's a feature or a bug, so we have to read the source code

    https://github.com/python/cpython/blob/main/Lib/ntpath.py

  2. Wulian233 commented on Sep 17, 2024

    @Wulian233
    Contributor
  3. matan1008 commented on Sep 17, 2024

    @matan1008
    ContributorAuthor

    I actually found the issue using pathlib, so I guess it is a serious API change. I have a PR ready to be pushed, if it is something you would like 😀

  4. terryjreedy commented on Sep 17, 2024

    @terryjreedy
    Member

    In CommandPrompt, cd C:Users fails. If same is true of os.chdir('C:Users'), then this seems a bug.

    EDIT: The above is not quite right as cd e:Languages worked in the sense that when I switched to the e drive, Languages had become the working directory there. But c:xxx seems 'Not found' even when 'xxx' exists.

  5. barneygale commented on Sep 17, 2024

    @barneygale
    Contributor

    https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file says:

    If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk. Examples of this format are as follows:

    • "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
    • "C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.
  6. zware commented on Sep 17, 2024

    @zware
    Member

    This is not a bug, it's an oddity of Windows file paths. On POSIX-ish platforms, there are relative (some/path/rooted/in/cwd/example.py) and absolute (/some/path/rooted/at/the/root/example.py) paths. On Windows each drive has its own current working directory (cwd), so there are relative (some\path\rooted\in\cwd\example.py), absolute (C:\some\path\on\drive_c\rooted\in\drive_c_root\example.py), drive-relative1 (C:some\path\on\drive_c\rooted\in\drive_c_cwd\example.py), and drive-agnostic-absolute1 (\some\path\on\current\drive\at\its\root\example.py). ntpath can't guess what kind you actually want, so if you want a rooted path you need to provide the root in addition to the drive.

    Footnotes

    1. Not sure what the real names should be ↩ ↩2

  7. removed
    type-bugAn unexpected behavior, bug, or error
    on Sep 17, 2024
  8. matan1008 commented on Sep 18, 2024

    @matan1008
    ContributorAuthor

    So in the above example (or in pathlib) what is the correct way to get PureWindowsPath('c:/x/y')? PureWindowsPath('c:') / '\\x' / 'y'?

  9. zware commented on Sep 18, 2024

    @zware
    Member

    That would be fine, as would PureWindowsPath('c:\\') / 'x' / 'y' or PureWindowsPath('c:') / '/' / 'x' / 'y'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions