Python pathlib and Filesystem Operations
Automation is mostly files. pathlib makes a path an object rather than a string, which means the filename's parts have names, joining uses / instead of string concatenation, and reading a whole file is one call. It also contains the single most valuable line in this domain: Path(__file__).resolve().parent, which is what stops a scheduled script losing track of its own data directory.
Files and Data Formats Guide 10 of 39 Beginner
- Control nodeUbuntu 26.04 LTS
- Python3.14.4
- requests2.34.2
- paramiko5.0.0
- TimeAbout 18 min
pathlib has been in the standard library since Python 3.4 and is the recommended interface. Path.with_stem arrived in 3.9 and unlink(missing_ok=True) in 3.8; both are used here. os.path still works and still appears in older code.
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
This guide includes
Use this because automation is mostly files. This matters because a scheduled script starts in a directory you did not choose - and one line makes every relative path in it survive that.
- treating a path as an object rather than a string, and joining with a slash
- asking the questions that come before touching anything
- writing the one line that survives a scheduler
- reading, writing, making and removing, in one call each
- finding files with glob, and separating
resolvefromabsolute
Before you start
- guide 9 - the working-directory problem this guide solves.
- Nothing else.
-
A path is an object, not a string
-
Joining with a slash
-
The questions you ask before touching anything
-
The one line that survives a scheduler
-
Read and write, in one call each
-
Making and removing directories
-
Finding files
-
resolve and absolute are not the same
-
What domain 2 asks about this