On this page
  1. errno
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Handling Specific Low-Level OSError Codes Portably
  7. Gotchas & Boundaries
  8. Constants & Attributes

errno

Prev: enum | Up: stdlib_module | Next: faulthandler
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — errno
Tags: #public #python3 #stdlib


Import

import errno

Overview

Standard POSIX/OS error code integer constants (ENOENT, EACCES, EEXIST, EAGAIN, EPIPE, ECONNREFUSED, ETIMEDOUT) and the reverse lookup map errno.errorcode. Prefer Python's built-in OSError subclasses (FileNotFoundError, PermissionError, TimeoutError) first; reach for errno when matching specific lower-level OS error codes on OSError.errno.

Quick Start

import errno

assert errno.errorcode[errno.ENOENT] == "ENOENT"
assert errno.errorcode[errno.EACCES] == "EACCES"

Implementation Walkthrough

1. Handling Specific Low-Level OSError Codes Portably

import errno

def is_transient_io_error(exc: OSError) -> bool:
    return exc.errno in {errno.EAGAIN, errno.EWOULDBLOCK, errno.EINTR, errno.ETIMEDOUT}

assert is_transient_io_error(OSError(errno.EAGAIN, "try again"))

Gotchas & Boundaries

Constants & Attributes

Linked from

Related notes