Hands-on Lab·Python Automation for IT
Python Encodings, Temp Files and Atomic Writes
Two problems that only show up in production. The first is encoding: `read_text()` uses whatever the locale says, which is not necessarily what wrote the file. The second is worse - a script that fails half way through a write leaves a truncated file where the good one used to be. This guide shows both, and the function that makes the second impossible.
Files and Data Formats Guide 15 of 39 Intermediate
- 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. **PEP 540** means the C locale now enables UTF-8 mode rather than degrading to ASCII, which this guide measures - so the classic locale failure is largely gone. `unlink(missing_ok=True)` needs **3.8**, and `os.replace` has been the portable rename since 3.3.
| 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
-
Which encoding a text file is read with
-
And what the same bytes decode to
-
errors=, for data you do not control
-
A temporary file that cleans itself up
-
The write that leaves a half-file behind
-
The atomic version
-
And on a run that succeeds
-
Why os.replace and not os.rename