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:
>>> import posix
>>> posix.uname().sysname
'Linux'
>>> posix.getcwd()
'/home/realpython'
Key Features
- Wraps the C library calls standardized by POSIX, such as
open(),fork(), theexecfamily,stat(), andwaitpid() - Exposes the process environment at interpreter startup through the
posix.environdictionary, whose keys and values arebyteson Unix - Defines the numeric constants used by those system calls, including file-mode flags, exit codes, and signal-related values
- Raises
OSErrorwhen 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:
>>> import posix
>>> posix.environ[b"HOME"]
b'/home/realpython'
Listing the entries of the current directory using the POSIX name:
>>> 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:
>>> 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
osmodule 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
environsnapshot 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:
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:
$ 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.
Related Resources
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.
For additional information on related topics, take a look at the following resources:
- The subprocess Module: Wrapping Programs With Python (Tutorial)
- How to Get a List of All Files in a Directory With Python (Tutorial)
- Build a Python Directory Tree Generator for the Command Line (Tutorial)
- Speed Up Your Python Program With Concurrency (Tutorial)
- Reading and Writing Files in Python (Guide) (Tutorial)
- Using Python's pathlib Module (Course)
- Python's pathlib Module: Taming the File System (Quiz)
- Using the Python subprocess Module (Course)
- Listing All Files in a Directory With Python (Course)
- Speed Up Python With Concurrency (Course)
- Python Concurrency (Quiz)
- Reading and Writing Files in Python (Course)
- Reading and Writing Files in Python (Quiz)
By Leodanis Pozo Ramos • Updated May 15, 2026