[英]Why does pathlib.Path(“C:”) resolve to the working directory on Windows?
Using Python 3.6 on Windows 7 x64, the path "C:"
seems identical to an empty path for Path.resolve()
:在 Windows 7 x64 上使用 Python 3.6,路径
"C:"
似乎与Path.resolve()
的空路径相同:
'Empty' paths are 'current working directory' cwd()
: “空”路径是“当前工作目录”
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:\\"
).我希望它将冒号(不带反斜杠)视为常规字符(对
Path("te:st")
),或者忽略它( "C"
),或者将路径视为驱动器根 ( "C:\\"
)。 But instead it seems to ignore the C altogether.但相反,它似乎完全忽略了 C。
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).对于其他驱动器号(
"A:"
、 "X:"
、...),resolve 要么无限期挂起(不好!)或要求我将磁盘插入驱动器(这表明它没有完全忽略驱动器号要么)。
It does not.它没有。
At least not in the sense that pathlib.Path("C:")
resolves to the working directory on Windows:至少不是
pathlib.Path("C:")
解析为 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:\\
:如您所见,
C:
解析C: 驱动器上的最后一个活动目录,这完全符合 Windows 使用C:
与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.因此,总而言之,
Path("C:").resolve()
行为与您预期的完全一样,基于长期建立的 Windows 行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.