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
- Python3.14.4
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- requests2.34.2
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
- TimeAbout 18 min
- Reviewed24 August 2026
Written against the versions above. `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.
| 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 |
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