Python中的 Pathlib 模块
Python中的Pathlib 模块提供了各种表示文件系统路径的类,这些类具有适用于不同操作系统的语义。该模块属于 Python 的标准实用程序模块。
Pathlib 模块中的Path classes
分为纯路径和具体路径。纯路径仅提供计算操作但不提供 I/O 操作,而从纯路径继承的具体路径提供计算操作和 I/O 操作。
纯粹的路径 –
如上所述,纯路径提供纯计算操作。纯路径类的对象提供了各种路径处理操作的方法。纯路径对象在不实际访问文件系统的情况下运行。
当我们只想操作路径而不实际访问操作系统时,纯路径很有用。我们可以通过实例化其中一个纯类轻松地在 Unix 机器上操作 Windows 文件系统路径,反之亦然。
类 pathlib.PurePath(*pathsegments) –
这是一个代表系统路径风格的通用类。实例化此类后,将创建pathlib.PurePosixPath或pathlib.PureWindowsPath
# Import PurePath class
# from pathlib module
from pathlib import PurePath
# Instantiate the PurePath class
obj = PurePath('foo/baar')
# print the instance of PurePath class
print(obj)
PurePosixPath('foo/baar')
类 pathlib.PurePosixPath(*pathsegments) –
这是PurePath类的子类。它表示非 Windows 文件系统路径。
# Import PurePosixPath class
# from pathlib module
from pathlib import PurePosixPath
# Instantiate the PurePosixPath class
obj = PurePosixPath('foo / baar')
# print the instance of PurePosixPath class
print(obj)
PurePosixPath('foo/baar')
类 pathlib.PureWindowsPath(*pathsegments) –
这也是patlib.PurePath类的子类。它代表 Windows 文件系统路径。
# Import PureWindowsPath class
# from pathlib module
from pathlib import PureWindowsPath
# Instantiate the PureWindowsPath class
obj = PureWindowsPath('foo / baar')
# print the instance of PureWindowsPath class
print(obj)
PureWindowsPath('foo/baar')
以下是 Pure Path 类提供的一些方法:
PurePath.is_absolute() 方法 –
此方法用于检查路径是否为绝对路径。如果路径是绝对的,则此方法返回 True,否则返回 False。
# Python program to explain PurePath.is_absolute() method
# Import PurePath class from pathlib module
from pathlib import PurePath
# Path
path = '/usr / local / bin'
# Instantiate the PurePath class
obj = PurePath(path)
# Check whether the given path is
# absolute or not
isAbs = obj.is_absolute()
print(isAbs)
True
PurePath.name 属性 –
此纯路径属性返回排除驱动器和根组件(如果有)后的最终路径组件。
# Python program to explain PurePath.name property
# Import PurePath class from pathlib module
from pathlib import PurePath
# Path
path = '/Desktop / file.txt'
# Instantiate the PurePath class
obj = PurePath(path)
# Get the final path component
comp = obj.name
print(comp)
file.txt
具体路径:
具体路径是纯路径类的子类。正如我们所知,纯路径类仅提供计算操作,但它也提供了各种方法来对路径对象执行系统调用。
我们可以通过以下三种方式实例化具体路径:
类 pathlib.Path(*pathsegments) –
这是pathlib.PurePath
类的子类。它代表了系统路径风格的具体路径。实例化后,此类将创建pathlib.PosixPath
或pathlib.WindowsPath
。
# Import the Path class
from pathlib import Path
# Instantiate the Path class
obj = Path('/usr/local/bin)
print(obj)
PosixPath('/usr/local/bin')
类 pathlib.PosixPath(*pathsegments) –
该类是pathlib.Path和pathlib.PurePosixPath类的子类。此类表示具体的非 Windows 文件系统路径。
注意:不能在 Windows 操作系统上实例化pathlib.Posixpath类。
# Import PosixPath class
# from pathlib module
from pathlib import PosixPath
# Instantiate the PosixPath class
obj = PosixPath('/usr/local/bin')
# Print the instance of PosixPath class
print(obj)
PosixPath('/usr/local/bin')
类 pathlib.WindowsPath(*pathsegments) –
此类是pathlib.Path和pathlib.PureWindowsPath的子类。此类表示具体的 Windows 文件系统路径。
# Import WindowsPath class
# from pathlib module
from pathlib import WindowsPath
# Instantiate the WindowsPath class
obj = WindowsPath('c:/Program Files/')
# print the instance of WindowsPath class
print(obj)
WindowsPath('c:/Program Files/')
下面是Path类提供的几个方法:
Path.cwd() method
:该方法返回一个代表当前工作目录的新路径对象。
# Import Path class
from pathlib import Path
# Get the current working directory name
cur_dir = Path.cwd()
print(cur_dir)
/home/ihritik
Path.exists() method
:此方法用于检查给定路径是否指向现有文件或目录。
# Import Path class
from pathlib import Path
# Path
path = '/home/ihritik/Desktop'
# Instantiate the Path class
obj = Path(path)
# Check if path points to
# an existing file or directory
print(obj.exists())
True
Path.is_dir() method
:此方法用于检查给定路径是否为目录。
# Import Path class
from pathlib import Path
# Path
path = '/home/ihritik/Desktop'
# Instantiate the Path class
obj = Path(path)
# Check if path refers to
# directory or not
print(obj.is_dir())
True
参考 - https://docs。 Python.org/3/library/pathlib.html