自动完成在 a 上找到的第一个方法pathlib.Path
是absolute()
.
它似乎只是在开头添加 Path.cwd():
>>> from pathlib import Path
>>> path = Path('./relative/path') / '../with/some/../relative/parts'
Path('relative/path/../with/some/../relative/parts')
# calling absolute...
>>> absolute_path = path.absolute()
Path('/[current_dir]/relative/path/../with/some/../relative/parts')
# does the same as prepending cwd at the start
>>> Path.cwd() / path
Path('/[current_dir]/relative/path/../with/some/../relative/parts')
但是,Path.absolute()
未在pathlib 文档中列出。
将此与Path.resolve()进行比较,后者的作用相反(替换相关部分但不前置cwd
)并 记录在案。
我可以使用absolute()
还是应该避免使用它?