The pathlib module, part of the Python standard library since version 3.4, offers an object-oriented way to interact with file system paths. Instead of representing paths as strings, pathlib uses Path objects, providing a more intuitive and cross-platform approach for file and directory operations. It streamlines tasks such as path joining, file reading and writing, and existence checks.
pathlib was created to address the complexities of file system manipulation in Python, offering a cleaner alternative to older methods using the os and os.path modules. Key features include platform-independent path handling, simplified reading and writing of files, easy directory content iteration, and convenient path combination using operators. It provides methods to inspect path properties like absolute status, file name, extension, and parent directory. The module also includes functionalities for creating directories, moving, copying, and deleting files.
As part of Python's standard library, pathlib is readily available in Python 3.4 and later versions. To use it, simply import it. There is no additional cost for using pathlib as it is included with Python.