On this page
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
- Never hardcode raw integers like
2or11: Numericerrnovalues vary across Linux, macOS, and Windows—always referenceerrno.ENOENT,errno.EAGAIN, etc. - Check
exc.errnoagainsterrno.*only when no specificOSErrorsubclass exists (e.g.,EIOon Linux PTY EOF orEXDEVon cross-device rename).
Constants & Attributes
errno.E2BIG(int) =7errno.EACCES(int) =13errno.EADDRINUSE(int) =48errno.EADDRNOTAVAIL(int) =49errno.EAFNOSUPPORT(int) =47errno.EAGAIN(int) =35errno.EALREADY(int) =37errno.EAUTH(int) =80errno.EBADARCH(int) =86errno.EBADEXEC(int) =85errno.EBADF(int) =9errno.EBADMACHO(int) =88errno.EBADMSG(int) =94errno.EBADRPC(int) =72errno.EBUSY(int) =16
Linked from
Related notes
- tuts/python3/stdlib_module/enum Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/faulthandler Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/getopt Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/io Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/os Linked here; shared tags: #stdlib
- tuts/python3/stdlib_module/time Linked here; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib