为什么pathlib.Path.parts中的第一个值是正斜杠?

问题描述 投票:0回答:1

如果我运行代码

from pathlib import Path
print(Path('/some/file/system/path/').parts)

将会输出

('/', 'some', 'file', 'system', 'path')

为什么第一个条目的值是正斜杠?

我在想也许它是为了与 Windows 兼容

C://
? (或者类似的东西,我不知道Windows到底是如何做事的)

或者也许这样你就可以区分相对路径和绝对路径?

python-3.x linux path filesystems
1个回答
1
投票

它是为了区分相对路径和绝对路径,如下所示:

>>> print(Path('/some/file/system/path/').parts) # absolute
('/', 'some', 'file', 'system', 'path')
>>> print(Path('some/file/system/path/').parts)  # relative
('some', 'file', 'system', 'path')
© www.soinside.com 2019 - 2024. All rights reserved.