Image by AI (Dalle-3)

A quick tour of the Python Pathlib library

Stop using os (mostly) and use Pathlib instead

--

Before Python version 3.4, when you had to refer to and perform operations on files and directories on your system, the library of choice was the built-in os module.

The os module has been the standard way of working with files and directories in Python for a long time. However, it has some limitations. For example, it didn't provide a unified way of working with paths across different operating systems. Additionally, manual string manipulation is required to construct paths, which could be error-prone.

That changed in version 3.4 when the Python developers introduced the pathlib library. The reasons for this varied, but the bottom line was that pathlib was more efficient, flexible, and easy to use. This made it an ideal choice for a wide range of applications and ensured that file operations could become platform-agnostic. Pathlib is designed to work seamlessly across different operating systems, including Windows, macOS, and Linux.

Note that the os module can still prove useful in certain situations e.g. checking file access permissions, walking directory structures etc …

Basic PathLib Operations

--

--

Thomas Reid

A data engineer , specialising in the AWS cloud with particular interest in AI, serverless and the energy, finance and insurance sectors