On this page
os.open
Prev: os.mkdir | Up: os | Next: os.pipe
Rel: os.abort, os.access, os.chdir, os.chmod
Ref: docs.python.org — os.open
Tags: #public #python3 #stdlib #os
Import
import os
from os import open
Signature & Overview
os.open(path, flags, mode=511, *, dir_fd=None)
Open a file for low level IO. Returns a file descriptor (integer).
Usage
import os
from pathlib import Path
import tempfile
with tempfile.TemporaryDirectory() as tmp:
p = Path(tmp) / "low.bin"
fd = os.open(p, os.O_CREAT | os.O_WRONLY | os.O_EXCL, 0o600)
try:
os.write(fd, b"hi")
finally:
os.close(fd)
assert p.read_bytes() == b"hi"
Gotchas
- Low-level POSIX fd open (NOT built-in
open()): Returns anintfile descriptor; use built-inopen()unless you specifically need flags likeO_EXCL,O_NOFOLLOW, orO_NONBLOCK.
REPL Inspect
from os import open
help(open)
Linked from
Related notes
- tuts/python3/stdlib_module/os/mkdir Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/pipe Linked here; links to this page; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/os/abort Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/access Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chdir Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chmod Linked here; shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/chown shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/close shared tags: #os, #stdlib
- tuts/python3/stdlib_module/os/cpu_count shared tags: #os, #stdlib