为什么 pathlib.Path("C:") 解析到 Windows 上的工作目录?
Why does pathlib.Path("C:") resolve to the working directory on Windows?
在 Windows 7 x64 上使用 Python 3.6,路径 "C:"
似乎与 Path.resolve()
:
的空路径相同
'Empty' 路径是 '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')
单个字母被解释为文件夹名称:
>>> Path(r"C").resolve()
WindowsPath('C:/Users/me/C')
一个完整的驱动器号 + 冒号 + 反斜杠按预期指向驱动器根目录:
>>>> Path(r"C:\").resolve()
WindowsPath('C:/')
但是忘记了反斜杠指向当前工作目录?
>>>> Path(r"C:").resolve()
WindowsPath('C:/Users/me/C')
我希望它要么将冒号(不带反斜杠)视为常规字符(它对 Path("te:st")
如此),要么忽略它 ("C"
),或者将路径作为驱动器根目录 ("C:\"
)。但它似乎完全忽略了 C。
对于其他驱动器盘符("A:"
、"X:"
、...),解决无限期挂起(不好!)或要求我将磁盘插入驱动器(这表明它也没有完全忽略驱动器号。
没有。
至少在 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')
>>>
如您所见,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)
...
与此比较:
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
...
这也适用于文件路径:
D:\>copy C:\.rnd %TEMP%
The system cannot find the file specified.
D:\>copy C:.rnd %TEMP%
1 file(s) copied.
同样地:
C:\Users\bersbers>D:
D:\>cd C:
C:\Users\bersbers
D:\>C:
C:\Users\bersbers>
对比
C:\Users\bersbers>D:
D:\>cd C:\
D:\>C:
C:\>
总而言之,Path("C:").resolve()
的行为完全符合您的预期,基于长期建立的 Windows 行为。
在 Windows 7 x64 上使用 Python 3.6,路径 "C:"
似乎与 Path.resolve()
:
'Empty' 路径是 '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')
单个字母被解释为文件夹名称:
>>> Path(r"C").resolve()
WindowsPath('C:/Users/me/C')
一个完整的驱动器号 + 冒号 + 反斜杠按预期指向驱动器根目录:
>>>> Path(r"C:\").resolve()
WindowsPath('C:/')
但是忘记了反斜杠指向当前工作目录?
>>>> Path(r"C:").resolve()
WindowsPath('C:/Users/me/C')
我希望它要么将冒号(不带反斜杠)视为常规字符(它对 Path("te:st")
如此),要么忽略它 ("C"
),或者将路径作为驱动器根目录 ("C:\"
)。但它似乎完全忽略了 C。
对于其他驱动器盘符("A:"
、"X:"
、...),解决无限期挂起(不好!)或要求我将磁盘插入驱动器(这表明它也没有完全忽略驱动器号。
没有。
至少在 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')
>>>
如您所见,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)
...
与此比较:
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
...
这也适用于文件路径:
D:\>copy C:\.rnd %TEMP%
The system cannot find the file specified.
D:\>copy C:.rnd %TEMP%
1 file(s) copied.
同样地:
C:\Users\bersbers>D:
D:\>cd C:
C:\Users\bersbers
D:\>C:
C:\Users\bersbers>
对比
C:\Users\bersbers>D:
D:\>cd C:\
D:\>C:
C:\>
总而言之,Path("C:").resolve()
的行为完全符合您的预期,基于长期建立的 Windows 行为。