On this page
posix
Prev: poplib | Up: stdlib_module | Next: posixpath
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — posix
Tags: #public #python3 #stdlib
Import
import posix
Overview
Low-level Unix/POSIX C-extension backing the os module on macOS, Linux, and BSD systems. Never import posix directly in portable code—always import os, which re-exports posix's functions alongside cross-platform wrappers like os.makedirs(), os.walk(), and os.environ.
Quick Start
import os
# os.name is "posix" on macOS/Linux/BSD; use os rather than importing posix directly
assert os.name in ("posix", "nt")
assert isinstance(os.getcwd(), str)
Implementation Walkthrough
1. Checking Platform Capabilities via os
If a script needs a POSIX-only syscall (such as os.fork, os.getuid, or os.statvfs), check hasattr(os, "getuid") or os.name == "posix" rather than importing posix directly so the module still imports cleanly on Windows.
Gotchas & Boundaries
- Unavailable on Windows:
import posixraisesModuleNotFoundErroron Windows (which uses nt instead). - Missing high-level helpers:
posixexposes raw syscalls (posix.stat,posix.listdir) but lacksos.path,os.environdecoding wrappers,os.walk(), andos.makedirs().
Common Functions
- posix.abort() —
abort()— Abort the interpreter immediately. - posix.access() —
access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True)— Use the real uid/gid to test for access to a path. - posix.chdir() —
chdir(path)— Change the current working directory to the specified path. - posix.chflags() —
chflags(path, flags, follow_symlinks=True)— Set file flags. - posix.chmod() —
chmod(...)— Change the access permissions of a file. - posix.chown() —
chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True)— Change the owner and group id of path to the numeric uid and gid. - posix.chroot() —
chroot(path)— Change root directory to path. - posix.close() —
close(fd)— Close a file descriptor. - posix.closerange() —
closerange(fd_low, fd_high, /)— Closes all file descriptors in [fd_low, fd_high), ignoring errors. - posix.confstr() —
confstr(name, /)— Return a string-valued system configuration variable. - posix.cpu_count() —
cpu_count()— Return the number of logical CPUs in the system. - posix.ctermid() —
ctermid()— Return the name of the controlling terminal for this process. - posix.device_encoding() —
device_encoding(fd)— Return a string describing the encoding of a terminal's file descriptor. - posix.dup() —
dup(fd, /)— Return a duplicate of a file descriptor. - posix.dup2() —
dup2(fd, fd2, inheritable=True)— Duplicate file descriptor. - posix.execv() —
execv(path, argv, /)— Execute an executable path with arguments, replacing current process. - posix.execve() —
execve(path, argv, env)— Execute an executable path with arguments, replacing current process. - posix.fchdir() —
fchdir(fd)— Change to the directory of the given file descriptor. - posix.fchmod() —
fchmod(fd, mode)— Change the access permissions of the file given by file descriptor fd. - posix.fchown() —
fchown(fd, uid, gid)— Change the owner and group id of the file specified by file descriptor. - posix.fork() —
fork()— Fork a child process. - posix.forkpty() —
forkpty()— Fork a new process with a new pseudo-terminal as controlling tty. - posix.fpathconf() —
fpathconf(fd, name, /)— Return the configuration limit name for the file descriptor fd. - posix.fspath() —
fspath(path)— Return the file system path representation of the object. - posix.fstat() —
fstat(fd)— Perform a stat system call on the given file descriptor. - posix.fstatvfs() —
fstatvfs(fd, /)— Perform an fstatvfs system call on the given fd. - posix.fsync() —
fsync(fd)— Force write of fd to disk. - posix.ftruncate() —
ftruncate(fd, length, /)— Truncate a file, specified by file descriptor, to a specific length. - posix.get_blocking() —
get_blocking(fd, /)— Get the blocking mode of the file descriptor. - posix.get_inheritable() —
get_inheritable(fd, /)— Get the close-on-exe flag of the specified file descriptor. - posix.get_terminal_size() —
get_terminal_size(...)— Return the size of the terminal window as (columns, lines). - posix.getcwd() —
getcwd()— Return a unicode string representing the current working directory. - posix.getcwdb() —
getcwdb()— Return a bytes string representing the current working directory. - posix.getegid() —
getegid()— Return the current process's effective group id. - posix.geteuid() —
geteuid()— Return the current process's effective user id.
Classes & Objects
- posix.DirEntry —
posix.DirEntryclass. (Methods:.inode(),.is_dir(),.is_file(),.is_junction(),.is_symlink(),.stat()) - posix.error — Base class for I/O related errors. (Methods:
.add_note(),.with_traceback()) - posix.stat_result — stat_result: Result from stat, fstat, or lstat. (Methods:
.count(),.index()) - posix.statvfs_result — statvfs_result: Result from statvfs or fstatvfs. (Methods:
.count(),.index()) - posix.terminal_size — A tuple of (columns, lines) for holding terminal window size (Methods:
.count(),.index()) - posix.times_result — times_result: Result from os.times(). (Methods:
.count(),.index()) - posix.uname_result — uname_result: Result from os.uname(). (Methods:
.count(),.index()) - posix.waitid_result — waitid_result: Result from waitid. (Methods:
.count(),.index())
Constants & Attributes
posix.CLD_CONTINUED(int) =6posix.CLD_DUMPED(int) =3posix.CLD_EXITED(int) =1posix.CLD_KILLED(int) =2posix.CLD_STOPPED(int) =5posix.CLD_TRAPPED(int) =4posix.EX_CANTCREAT(int) =73posix.EX_CONFIG(int) =78posix.EX_DATAERR(int) =65posix.EX_IOERR(int) =74posix.EX_NOHOST(int) =68posix.EX_NOINPUT(int) =66posix.EX_NOPERM(int) =77posix.EX_NOUSER(int) =67posix.EX_OK(int) =0
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/nt
- tuts/python3/stdlib_module/poplib
- tuts/python3/stdlib_module/posix/abort
- tuts/python3/stdlib_module/posix/access
- tuts/python3/stdlib_module/posix/chdir
- tuts/python3/stdlib_module/posix/chflags
- tuts/python3/stdlib_module/posix/chmod
- tuts/python3/stdlib_module/posix/chown
- tuts/python3/stdlib_module/posix/chroot
- tuts/python3/stdlib_module/posix/close
- tuts/python3/stdlib_module/posix/closerange
- tuts/python3/stdlib_module/posix/confstr
- tuts/python3/stdlib_module/posix/cpu_count
- tuts/python3/stdlib_module/posix/ctermid
- tuts/python3/stdlib_module/posix/device_encoding
- tuts/python3/stdlib_module/posix/DirEntry
- tuts/python3/stdlib_module/posix/dup
- tuts/python3/stdlib_module/posix/dup2
- tuts/python3/stdlib_module/posix/error
- tuts/python3/stdlib_module/posix/execv
- tuts/python3/stdlib_module/posix/execve
- tuts/python3/stdlib_module/posix/fchdir
- tuts/python3/stdlib_module/posix/fchmod
- tuts/python3/stdlib_module/posix/fchown
- tuts/python3/stdlib_module/posix/fork
- tuts/python3/stdlib_module/posix/forkpty
- tuts/python3/stdlib_module/posix/fpathconf
- tuts/python3/stdlib_module/posix/fspath
- tuts/python3/stdlib_module/posix/fstat
- tuts/python3/stdlib_module/posix/fstatvfs
- tuts/python3/stdlib_module/posix/fsync
- tuts/python3/stdlib_module/posix/ftruncate
- tuts/python3/stdlib_module/posix/get_blocking
- tuts/python3/stdlib_module/posix/get_inheritable
- tuts/python3/stdlib_module/posix/get_terminal_size
- tuts/python3/stdlib_module/posix/getcwd
- tuts/python3/stdlib_module/posix/getcwdb
- tuts/python3/stdlib_module/posix/getegid
- tuts/python3/stdlib_module/posix/geteuid
- tuts/python3/stdlib_module/posix/stat_result
- tuts/python3/stdlib_module/posix/statvfs_result
- tuts/python3/stdlib_module/posix/terminal_size
- tuts/python3/stdlib_module/posix/times_result
- tuts/python3/stdlib_module/posix/uname_result
- tuts/python3/stdlib_module/posix/waitid_result
- tuts/python3/stdlib_module/posixpath
- tuts/python3/stdlib_module/stdlib_module
Related notes
- tuts/python3/stdlib_module/nt Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/poplib Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/abort Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/access Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/chdir Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/chflags Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/chmod Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/chown Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/chroot Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/posix/close Linked here; links to this page; shared tags: #stdlib