How to Read a File with Retry on Temporary IOError in Python

Read a file with automatic retries on temporary IOError/OSError failures, using the pathlib module with configurable attempts and delay.

Easy Python 3.9+ Aug 9, 2026 Files & data 14 views 0 copies

Python code

29 lines
Python 3.9+
import time
from pathlib import Path

def read_file_with_retry(filepath: str | Path, max_attempts: int = 3, delay: float = 0.5) -> str:
    """Read a file with retries on temporary IO errors."""
    path = Path(filepath)
    last_error = None

    for attempt in range(max_attempts):
        try:
            return path.read_text()
        except (IOError, OSError) as e:
            last_error = e
            if attempt < max_attempts - 1:
                print(f"Attempt {attempt + 1} failed: {e}. Retrying in {delay} seconds...")
                time.sleep(delay)

    raise last_error


if __name__ == "__main__":
    example_file = Path("temp_retry_example.txt")
    example_file.write_text("File content loaded successfully.\n")

    try:
        content = read_file_with_retry(example_file, max_attempts=3)
        print(content, end="")
    finally:
        example_file.unlink(missing_ok=True)

Output

stdout
File content loaded successfully.

How it works

The function wraps Path.read_text() inside a loop that catches IOError and OSError exceptions. On each failure, it records the last error, prints a warning if retries remain, and sleeps for the specified delay. After exhausting all attempts, it re-raises the last caught exception so the caller can handle it. This keeps the code simple while deferring the retry policy to a single reusable function.

Common mistakes

  • Catching `IOError` but not `OSError` — on modern Python they are aliases, but catching both is safer.
  • Not checking if the file exists first, causing immediate failures instead of useful retries.
  • Sleeping on the final failed attempt when no retry will occur.

Variations

  1. Use `Retry` from the `tenacity` library for more advanced backoff strategies.
  2. Wrap the function in a decorator to add retry logic to any file operation.

Real-world use cases

  • Reading configuration files at startup when a shared volume might momentarily be unavailable.
  • Fetching data from a network-mounted filesystem where transient connection errors are common.
  • Processing batch jobs that read a file immediately after it is written by another service, avoiding race conditions.

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.