How to Write a List of Lines to a Text File Safely in Python
This code atomically writes a list of strings as lines to a text file using a temporary file and os.replace to prevent corruption.
Python code
23 linesfrom pathlib import Path
import tempfile
import os
def write_lines_safely(lines: list[str], filepath: str | Path) -> None:
"""Write lines to a text file atomically to avoid corruption."""
path = Path(filepath)
path.parent.mkdir(parents=True, exist_ok=True)
fd, temp_path = tempfile.mkstemp(dir=str(path.parent), suffix=".tmp")
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.writelines(line + "\n" for line in lines)
os.replace(temp_path, path)
except Exception:
os.unlink(temp_path)
raise
if __name__ == "__main__":
lines = ["alpha", "beta", "gamma"]
target = "output/sample.txt"
write_lines_safely(lines, target)
print(Path(target).read_text())
Output
alpha
beta
gamma
How it works
The function creates a temporary file in the same directory as the target using tempfile.mkstemp. It writes all lines with newline characters, then atomically renames the temp file to the target with os.replace. This ensures the file is never left partially written if the process crashes. path.parent.mkdir creates missing directories. If any exception occurs during writing, the temp file is cleaned up before re-raising.
Common mistakes
- Forgetting to add a newline character to each line, causing lines to be concatenated.
- Not cleaning up the temporary file on failure, leaving stray .tmp files.
- Using `os.rename` instead of `os.replace` on Windows, which fails if the target exists.
Variations
- Use `pathlib.Path.write_text` with a joined string for simpler but non-atomic writes.
- Wrap the write in a context manager and use `os.replace` directly with a manually created temp file.
Real-world use cases
- Saving configuration files where a partial write could leave the app unusable.
- Writing log or export files to shared storage where readers must not see partial content.
- Updating system files or artifacts in CI/CD pipelines atomically to avoid corrupt deployments.
Sponsored
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.