1.PosixPath 和WindowPath
PosixPath 和 WindowPath 是 Path 的子类,分别用于 Unix风格的路径和 Windows 风格的路径。
示例1.1:(ubuntu)
from pathlib import *
path1 = PosixPath('C:', 'root', 'file.txt')
path2 = PosixPath('C:/', 'root', 'file.txt')
print(path1)
print(path2)
print()
path3 = PureWindowsPath('C:', 'root', 'file.txt')
path4 = PureWindowsPath('C:/', 'root', 'file.txt')
print(path3)
print(path4)
输出结果:
C:/root/file.txt
C:/root/file.txt
C:root\file.txt
C:\root\file.txt
注意:在Linux操作系统下无法使用WindowPath,会报错:NotImplementedError: cannot instantiate 'WindowsPath' on your system
,必须使用PureWindowsPath,同理在window下无法使用PosixPath得使用PurePosixPath。
示例1.2:用 / 连接
path1 = PosixPath('C:', 'root', 'file.txt')
path2 = PureWindowsPath('C:', 'root', 'file.txt')
print(path1 / 'das')
print(path2 / 'das')
输出结果:
C:/root/file.txt/das
C:root\file.txt\das
2.PurePosixPath和PureWindowsPath
PurePosixPath 和 PureWindowsPath 是 PurePath 的子类,也是分别用于 Unix风格的路径和 Windows 风格的路径,用于在不同系统之下可以输出另一个系统风格的路径。另外,path同样是PurePath的子类,PosixPath 和WindowPath同样分别是PurePosixPath和PureWindowsPath的子类。