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.

Medium Python 3.9+ Aug 9, 2026 Files & data 13 views 0 copies

Python code

23 lines
Python 3.9+
from 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

stdout
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

  1. Use `pathlib.Path.write_text` with a joined string for simpler but non-atomic writes.
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.