<< plib_os

Python Library pathlib

pathlib Introduction

pathlib is a object-oriented interface to manipulate paths of operating system. Refer to os.path module for The underling low-level operation on paths based on string. pathlib is not designed for recursive directory operation such as directory copying or deleting, refer to shutil for these recursive directory operations.

pathlib construct two level hierarchy of path, the pure path and concrete path, the difference is that pure path does not aware of on the actual filesystem but only the abstract string-operating provided, and the concrete path inherit from pure path equipped with file operation support. For each type, the path under Windows and POSIX are also distinguished (but you can also leave it up to Python to decide), hence there are four classes of Path: PurePath, PurePosixPath, PureWindowsPath, and Path, PosixPath, WindowsPath.

For the most common usage, just use Path by which you could manipulate the file system. For some special cases if you intend to avoid the actual filesystem accessing, or you want to manipulate Windowspath in Unix system, use pure path instead.

Creating the path object of current working directory is as simple as invoking the constructor, note that when creating PurePath or Path it will be automatically converted to the corresponding specific type based on operating system.

>>> from pathlib import PurePath, Path
>>> PurePath('.')
PosixPurePath(',')
>>> Path('.')
PosixPath('.')

pathlib.PurePath

The PurePath class provides symbolic operation on path:

Note that since PurePath is unaware of the real filesytem, these operation are pure logical operation, for example,

>>> PurePosixPath('foo/..').parent
PurePosixPath('foo')

to walk an arbitary path with multiple special notation included, it is recommended to first call Path.resolve() so as to resolve symlinks and eliminate .. components.

Refer to pure_path_asserts.py for examples about PurePath properties.

pathlib.Path

As mentioned, Path inherits PurePath and equipped with actual filesystem.

pathlib.Path File Inforamtion Retriving

Checking of seven types of Unix file type are encapsulated as dependent functions:

All the seven checking functions also return False if the path does not exist at all. Other errors such as permission errors are propagated.

Refer to unix_file_intro for the details about the file of Unix.

Besides the file type checking, there are other functions that check the properties of a path:

pathlib.Path Directory Iteration

There are several ways to iterate the directory: