Skip to content

ntpath not adding slash after drive letters #124167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
matan1008 opened this issue Sep 17, 2024 · 8 comments
Closed

ntpath not adding slash after drive letters #124167

matan1008 opened this issue Sep 17, 2024 · 8 comments

Comments

@matan1008
Copy link
Contributor

matan1008 commented Sep 17, 2024

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

@matan1008 matan1008 added the type-bug An unexpected behavior, bug, or error label Sep 17, 2024
@Wulian233
Copy link
Contributor

Wulian233 commented Sep 17, 2024

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

@Wulian233
Copy link
Contributor

@matan1008
Copy link
Contributor Author

matan1008 commented Sep 17, 2024

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 😀

@terryjreedy
Copy link
Member

terryjreedy commented Sep 17, 2024

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.

@barneygale
Copy link
Contributor

barneygale commented Sep 17, 2024

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.

@zware
Copy link
Member

zware commented Sep 17, 2024

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

@zware zware closed this as not planned Won't fix, can't repro, duplicate, stale Sep 17, 2024
@zware zware removed the type-bug An unexpected behavior, bug, or error label Sep 17, 2024
@matan1008
Copy link
Contributor Author

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

@zware
Copy link
Member

zware commented Sep 18, 2024

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
Projects
None yet
Development

No branches or pull requests

5 participants