Why does pathlib.Path(“C:”) resolve to the working directory on Windows?

孤人 提交于 2021-01-27 17:59:04

问题


Using Python 3.6 on Windows 7 x64, the path "C:" seems identical to an empty path for Path.resolve():

'Empty' paths are 'current working directory' cwd():

>>> from pathlib import Path
>>> Path().resolve()
WindowsPath('C:/Users/me')
>>> Path(r"").resolve()
WindowsPath('C:/Users/me')
>>> Path.cwd().resolve()
WindowsPath('C:/Users/me')

A single letter is interpreted as a folder name:

>>> Path(r"C").resolve()
WindowsPath('C:/Users/me/C')

A full blown drive-letter + colon + backslash points to the drive root as expected:

>>>> Path(r"C:\").resolve()
WindowsPath('C:/')

But forgetting the backslash points back to the current work directory?

>>>> Path(r"C:").resolve()
WindowsPath('C:/Users/me/C')

I would expect it to either treat the colon (without a backslash) as a regular character (it does so for Path("te:st")), or either ignore it ("C"), or treat the path as the drive root ("C:\"). But instead it seems to ignore the C altogether.

For other drive letters ("A:", "X:", ...), resolve either hangs indefinitely (not nice!) or asks me to insert a disk into the drive (which indicates that it's not completely ignoring the drive letter either).


回答1:


It does not.

At least not in the sense that pathlib.Path("C:") resolves to the working directory on Windows:

C:\Users\bersbers>d:

D:\>python
Python 3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 23:11:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pathlib import Path
>>> Path.cwd().resolve()
WindowsPath('D:/')
>>> Path(r"C:").resolve()
WindowsPath('C:/Users/bersbers')
>>>

As you can see, C: resolves the last active directory on the C: drive, which is completely in line with how Windows uses C: vs. C:\:

D:\>dir C:\
 Volume in drive C is Windows
 Volume Serial Number is 1234-ABCD

 Directory of C:\

01/17/2020  10:34 AM    <DIR>          Program Files
01/18/2020  12:11 AM    <DIR>          Program Files (x86)
...

Compare that to this:

D:\>dir C:
 Volume in drive C is Windows
 Volume Serial Number is 1234-ABCD

 Directory of C:\Users\bersbers

01/20/2020  11:19 AM    <DIR>          .
01/20/2020  11:19 AM    <DIR>          ..
08/23/2018  10:45 AM    <DIR>          .cache
11/27/2019  11:26 PM             1,024 .rnd
...

This also applies to file paths:

D:\>copy C:\.rnd %TEMP%
The system cannot find the file specified.

D:\>copy C:.rnd %TEMP%
        1 file(s) copied.

And similarly:

C:\Users\bersbers>D:

D:\>cd C:
C:\Users\bersbers

D:\>C:

C:\Users\bersbers>

versus

C:\Users\bersbers>D:

D:\>cd C:\

D:\>C:

C:\>

So in summary, Path("C:").resolve() behaves exactly as you would expect it to, based on long-established Windows behavior.



来源:https://stackoverflow.com/questions/48810950/why-does-pathlib-pathc-resolve-to-the-working-directory-on-windows

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!