Pick exactly one access mode — O_RDONLY, O_WRONLY or O_RDWR (0, 1 and 2) — and OR in the extras. O_RDONLY, O_WRONLY, O_RDWR, O_APPEND, O_CREAT, O_EXCL and O_TRUNC exist on Unix and Windows; the rest are Unix-only, Windows-only or Linux extensions. Always use the names: only the three access modes have the same value everywhere.
os constantsPython 3.0+ (O_CLOEXEC 3.3+, O_PATH and O_TMPFILE 3.4+)
Common call
os.O_WRONLY | os.O_CREAT | os.O_TRUNC
Returns
int bit mask for os.open
Replaces
the mode strings of open(): 'r', 'w', 'a', 'x', '+'
Watch out
Unix-only and Windows-only flags: guard with getattr(os, 'O_BINARY', 0)
O_DSYNC, O_RSYNC, O_SYNC, O_NDELAY, O_NONBLOCK, O_NOCTTY, O_CLOEXEC (3.3+). O_FSYNC is listed for macOS in the docs but Linux has it too (equal to O_SYNC there)
Windows only
O_BINARY, O_NOINHERIT, O_SHORT_LIVED, O_TEMPORARY (the file is deleted when the fd is closed), O_RANDOM, O_SEQUENTIAL, O_TEXT
C library extensions
O_ASYNC, O_DIRECT, O_DIRECTORY, O_NOFOLLOW, O_NOATIME, O_PATH (3.4+), O_TMPFILE (3.4+, Linux 3.11+) — present only where the C library defines them. O_ACCMODE (mask for the access mode, 3 on Linux) and O_LARGEFILE (0 on 64-bit Linux) are exported on Linux but not documented
Values
Only O_RDONLY = 0, O_WRONLY = 1, O_RDWR = 2 agree across Linux and Windows; for example O_CREAT is 64 on Linux and 256 on Windows, O_APPEND 1024 vs 8
Aliases on Linux
O_NDELAY == O_NONBLOCK, and O_SYNC == O_RSYNC == O_FSYNC
O_BINARY, O_TEXT, O_NOINHERIT, O_TEMPORARY, O_SHORT_LIVED, O_RANDOM and O_SEQUENTIAL are Windows-only (docs.python.org lists them as such). Unix has no text mode, so there is nothing to switch off. Write getattr(os, 'O_BINARY', 0) and the same flags work on every system. This page has no live demo because the set of flags and their values depend on the platform; the examples only use names and values that agree on Linux and Windows.