Skip to content

posix

The Python posix module provides a low-level, Unix-only interface to operating system services defined by POSIX, the Portable Operating System Interface standard. Python programs should import the os module instead, which re-exports every posix name on Unix and offers a portable interface that also works on Windows.

The module is still importable on Unix systems and exposes the same functions under their POSIX names:

Language: Python
>>> import posix

>>> posix.uname().sysname
'Linux'
>>> posix.getcwd()
'/home/realpython'

Key Features

  • Wraps the C library calls standardized by POSIX, such as open(), fork(), the exec family, stat(), and waitpid()
  • Exposes the process environment at interpreter startup through the posix.environ dictionary, whose keys and values are bytes on Unix
  • Defines the numeric constants used by those system calls, including file-mode flags, exit codes, and signal-related values
  • Raises OSError when a system call fails and lets type errors propagate as usual

Frequently Used Classes and Functions

Object Type Description
posix.environ Attribute Holds the process environment captured when the interpreter started, with bytes keys and values on Unix
posix.uname() Function Returns information identifying the current operating system
posix.getcwd() Function Returns the current working directory as a string
posix.listdir() Function Returns a list of the entries in a given directory
posix.stat() Function Performs the POSIX stat system call and returns a stat_result object
posix.fork() Function Forks the current process and returns the child PID in the parent and 0 in the child
posix.DirEntry Class Represents a directory entry yielded by scandir()

Examples

Reading a variable from the startup environment, which is keyed by bytes on Unix:

Language: Python
>>> import posix

>>> posix.environ[b"HOME"]
b'/home/realpython'

Listing the entries of the current directory using the POSIX name:

Language: Python
>>> import posix

>>> sorted(posix.listdir(b"."))[:3]
[b'README.md', b'pyproject.toml', b'src']

Calling the preferred portable equivalents from the os module, which forwards to posix on Unix:

Language: Python
>>> import os, posix

>>> os.getcwd is posix.getcwd
True
>>> os.getcwd()
'/home/realpython'

Common Use Cases

The most common reasons to care about posix include:

  • Understanding how the os module delegates to operating system calls on Unix
  • Reading POSIX-specific constants and error codes that appear in tracebacks
  • Auditing or teaching how Python exposes low-level system calls
  • Accessing the frozen environ snapshot captured before any runtime modifications

Real-World Example

Consider an audit script that lists the POSIX system calls an application touches indirectly through the os module:

Language: Python Filename: audit_posix.py
import os
import posix

os_names = {name for name in dir(os) if not name.startswith("_")}
posix_names = {name for name in dir(posix) if not name.startswith("_")}

shared = sorted(os_names & posix_names)
os_only = sorted(os_names - posix_names)

print(f"Shared with posix: {len(shared)}")
print(f"Added by os only:  {len(os_only)}")
print("First shared:", shared[:3])
print("First os-only:", os_only[:3])

Running the script on a Linux system prints a small report:

Language: Shell
$ python audit_posix.py
Shared with posix: 312
Added by os only:  58
First shared: ['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED']
First os-only: ['GenericAlias', 'Mapping', 'MutableMapping']

The shared set is exactly the surface that os inherits from posix, while the os-only names are the portable extras the standard library layers on top.

Tutorial

Python's pathlib Module: Taming the File System

Python's pathlib module enables you to handle file and folder paths in a modern way. This built-in module provides intuitive semantics that work the same way on different operating systems. In this tutorial, you'll get to know pathlib and explore common tasks when interacting with paths.

intermediate python stdlib

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated May 15, 2026