The most commonly used standard library modules that form the foundation of Python development. These modules provide core functionality for system interface, text processing, data structures, and file operations.
Core modules for interacting with the operating system and Python runtime environment.
import os
import sys
import platform
import errno# Environment variables
os.environ: dict # Environment variables mapping
def getenv(key: str, default: str = None) -> str: ...
def putenv(key: str, value: str) -> None: ...
# File system operations
def getcwd() -> str: ...
def chdir(path: str) -> None: ...
def listdir(path: str = '.') -> list: ...
def mkdir(path: str, mode: int = 0o777) -> None: ...
def makedirs(name: str, mode: int = 0o777, exist_ok: bool = False) -> None: ...
def remove(path: str) -> None: ... # Remove file
def rmdir(path: str) -> None: ... # Remove directory
def removedirs(name: str) -> None: ... # Remove directories recursively
def rename(src: str, dst: str) -> None: ...
def stat(path: str) -> os.stat_result: ...
def walk(top: str, topdown: bool = True, onerror=None, followlinks: bool = False): ...
# Path operations (os.path module)
os.path.join(path1: str, *paths: str) -> str # Join paths
os.path.split(path: str) -> tuple # Split into (head, tail)
os.path.splitext(path: str) -> tuple # Split into (root, ext)
os.path.basename(path: str) -> str # Get filename part
os.path.dirname(path: str) -> str # Get directory part
os.path.exists(path: str) -> bool # Check if path exists
os.path.isfile(path: str) -> bool # Check if path is file
os.path.isdir(path: str) -> bool # Check if path is directory
os.path.getsize(path: str) -> int # Get file size in bytes
os.path.abspath(path: str) -> str # Get absolute path
# Process management
def system(command: str) -> int: ... # Execute system command
def getpid() -> int: ... # Get process ID
def getppid() -> int: ... # Get parent process ID# System information
sys.version: str # Python version string
sys.version_info: tuple # Version as named tuple
sys.platform: str # Platform identifier
sys.executable: str # Python interpreter path
sys.prefix: str # Installation prefix
sys.path: list # Module search path
# Command line arguments
sys.argv: list # Command line arguments
# Standard streams
sys.stdin: TextIO # Standard input
sys.stdout: TextIO # Standard output
sys.stderr: TextIO # Standard error
# Functions
def exit(code: int = 0) -> None: ... # Exit interpreter
def getsizeof(obj) -> int: ... # Get object size in bytes
def getrefcount(obj) -> int: ... # Get reference countdef system() -> str: ... # Operating system name
def machine() -> str: ... # Machine type
def processor() -> str: ... # Processor name
def python_version() -> str: ... # Python version
def platform(aliased: bool = False, terse: bool = False) -> str: ... # Platform stringModules for processing and manipulating text data.
import re
import string
import textwrap# Pattern compilation
def compile(pattern: str, flags: int = 0) -> Pattern: ...
# Matching functions
def match(pattern: str, string: str, flags: int = 0) -> Match | None: ...
def search(pattern: str, string: str, flags: int = 0) -> Match | None: ...
def findall(pattern: str, string: str, flags: int = 0) -> list: ...
def finditer(pattern: str, string: str, flags: int = 0): ...
# Substitution functions
def sub(pattern: str, repl: str, string: str, count: int = 0, flags: int = 0) -> str: ...
def subn(pattern: str, repl: str, string: str, count: int = 0, flags: int = 0) -> tuple: ...
# Utility functions
def split(pattern: str, string: str, maxsplit: int = 0, flags: int = 0) -> list: ...
def escape(pattern: str) -> str: ...
# Pattern object
class Pattern:
def match(self, string: str, pos: int = 0, endpos: int = None) -> Match | None: ...
def search(self, string: str, pos: int = 0, endpos: int = None) -> Match | None: ...
def findall(self, string: str, pos: int = 0, endpos: int = None) -> list: ...
def finditer(self, string: str, pos: int = 0, endpos: int = None): ...
def sub(self, repl: str, string: str, count: int = 0) -> str: ...
def split(self, string: str, maxsplit: int = 0) -> list: ...
# Match object
class Match:
def group(self, *groups) -> str: ...
def groups(self, default: str = None) -> tuple: ...
def groupdict(self, default: str = None) -> dict: ...
def start(self, group: int = 0) -> int: ...
def end(self, group: int = 0) -> int: ...
def span(self, group: int = 0) -> tuple: ...
@property
def string(self) -> str: ...
@property
def pos(self) -> int: ...
@property
def endpos(self) -> int: ...
# Flags
IGNORECASE: int # Case insensitive matching
MULTILINE: int # ^ and $ match line boundaries
DOTALL: int # . matches newlines
VERBOSE: int # Allow verbose regexes# String constants
string.ascii_letters: str # 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.ascii_lowercase: str # 'abcdefghijklmnopqrstuvwxyz'
string.ascii_uppercase: str # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
string.digits: str # '0123456789'
string.hexdigits: str # '0123456789abcdefABCDEF'
string.octdigits: str # '01234567'
string.punctuation: str # Punctuation characters
string.printable: str # All printable characters
string.whitespace: str # All whitespace characters
# String formatting
class Template:
def __init__(self, template: str) -> None: ...
def substitute(self, mapping=None, **kwds) -> str: ...
def safe_substitute(self, mapping=None, **kwds) -> str: ...Specialized container datatypes and algorithms for working with collections.
import collections
import collections.abc
import heapq
import bisect# Specialized containers
class defaultdict(dict):
def __init__(self, default_factory=None, *args, **kwargs) -> None: ...
@property
def default_factory(self): ...
class Counter(dict):
def __init__(self, iterable=None, **kwds) -> None: ...
def most_common(self, n: int = None) -> list: ...
def elements(self): ...
def subtract(self, iterable=None, **kwds) -> None: ...
def update(self, iterable=None, **kwds) -> None: ...
class deque:
def __init__(self, iterable=None, maxlen: int = None) -> None: ...
def append(self, x) -> None: ...
def appendleft(self, x) -> None: ...
def pop(self): ...
def popleft(self): ...
def extend(self, iterable) -> None: ...
def extendleft(self, iterable) -> None: ...
def rotate(self, n: int = 1) -> None: ...
def clear(self) -> None: ...
def copy(self) -> deque: ...
@property
def maxlen(self) -> int: ...
def namedtuple(typename: str, field_names, rename: bool = False,
defaults=None, module=None) -> type: ...
class OrderedDict(dict):
def move_to_end(self, key, last: bool = True) -> None: ...
def popitem(self, last: bool = True) -> tuple: ...
class ChainMap(dict):
def __init__(self, *maps) -> None: ...
@property
def maps(self) -> list: ...
def new_child(self, m=None) -> ChainMap: ...
@property
def parents(self) -> ChainMap: ...def heappush(heap: list, item) -> None: ...
def heappop(heap: list): ...
def heappushpop(heap: list, item): ...
def heapreplace(heap: list, item): ...
def heapify(x: list) -> None: ...
def nlargest(n: int, iterable, key=None) -> list: ...
def nsmallest(n: int, iterable, key=None) -> list: ...def bisect_left(a: list, x, lo: int = 0, hi: int = None) -> int: ...
def bisect_right(a: list, x, lo: int = 0, hi: int = None) -> int: ...
def bisect(a: list, x, lo: int = 0, hi: int = None) -> int: ...
def insort_left(a: list, x, lo: int = 0, hi: int = None) -> None: ...
def insort_right(a: list, x, lo: int = 0, hi: int = None) -> None: ...
def insort(a: list, x, lo: int = 0, hi: int = None) -> None: ...Tools for functional programming paradigms and higher-order functions.
import itertools
import functools
import operator# Infinite iterators
def count(start: int = 0, step: int = 1): ...
def cycle(iterable): ...
def repeat(object, times=None): ...
# Finite iterators
def accumulate(iterable, func=None, initial=None): ...
def chain(*iterables): ...
def chain.from_iterable(iterable): ...
def compress(data, selectors): ...
def dropwhile(predicate, iterable): ...
def filterfalse(predicate, iterable): ...
def groupby(iterable, key=None): ...
def islice(iterable, start, stop=None, step=None): ...
def starmap(function, iterable): ...
def takewhile(predicate, iterable): ...
def tee(iterable, n: int = 2) -> tuple: ...
def zip_longest(*iterables, fillvalue=None): ...
# Combinatorial iterators
def product(*iterables, repeat: int = 1): ...
def permutations(iterable, r=None): ...
def combinations(iterable, r): ...
def combinations_with_replacement(iterable, r): ...def reduce(function, iterable, initializer=None): ...
# Decorators
def lru_cache(maxsize: int = 128, typed: bool = False): ...
def cache(user_function): ...
def wraps(wrapped): ...
# Partial application
class partial:
def __init__(self, func, *args, **keywords) -> None: ...
@property
def func(self): ...
@property
def args(self) -> tuple: ...
@property
def keywords(self) -> dict: ...
def partialmethod(func, *args, **keywords): ...
# Comparison functions
def cmp_to_key(func): ...
# Generic functions
def singledispatch(func): ...
def singledispatchmethod(func): ...# Arithmetic operators
def add(a, b): ...
def sub(a, b): ...
def mul(a, b): ...
def truediv(a, b): ...
def floordiv(a, b): ...
def mod(a, b): ...
def pow(a, b): ...
def neg(a): ...
def pos(a): ...
def abs(a): ...
# Comparison operators
def eq(a, b) -> bool: ...
def ne(a, b) -> bool: ...
def lt(a, b) -> bool: ...
def le(a, b) -> bool: ...
def gt(a, b) -> bool: ...
def ge(a, b) -> bool: ...
# Logical operators
def and_(a, b): ...
def or_(a, b): ...
def not_(a): ...
def xor(a, b): ...
# Sequence operations
def getitem(obj, key): ...
def setitem(obj, key, value) -> None: ...
def delitem(obj, key) -> None: ...
def contains(a, b) -> bool: ...
# Attribute operations
def getattr(obj, name, default=None): ...
def setattr(obj, name, value) -> None: ...
def delattr(obj, name) -> None: ...
def attrgetter(*attrs): ...
def itemgetter(*items): ...
def methodcaller(name, *args, **kwargs): ...Modern and legacy approaches to file system operations and path manipulation.
import pathlib
import os.path
import shutil
import glob
import tempfileclass Path:
def __init__(self, *args) -> None: ...
# Properties
@property
def name(self) -> str: ... # Final component
@property
def suffix(self) -> str: ... # File extension
@property
def suffixes(self) -> list: ... # All extensions
@property
def stem(self) -> str: ... # Final component without suffix
@property
def parent(self) -> Path: ... # Parent directory
@property
def parents(self): ... # Sequence of parents
@property
def anchor(self) -> str: ... # Anchor (drive/root)
@property
def parts(self) -> tuple: ... # Path components
# Methods
def __truediv__(self, other) -> Path: ... # Path joining with /
def joinpath(self, *args) -> Path: ...
def match(self, pattern: str) -> bool: ...
def is_absolute(self) -> bool: ...
def is_relative_to(self, *other) -> bool: ...
def resolve(self, strict: bool = False) -> Path: ...
def absolute(self) -> Path: ...
def relative_to(self, *other) -> Path: ...
def with_name(self, name: str) -> Path: ...
def with_suffix(self, suffix: str) -> Path: ...
def with_stem(self, stem: str) -> Path: ...
# File system operations
def exists(self) -> bool: ...
def is_file(self) -> bool: ...
def is_dir(self) -> bool: ...
def is_symlink(self) -> bool: ...
def stat(self) -> os.stat_result: ...
def lstat(self) -> os.stat_result: ...
def owner(self) -> str: ...
def group(self) -> str: ...
def chmod(self, mode: int) -> None: ...
# Directory operations
def iterdir(self): ...
def glob(self, pattern: str): ...
def rglob(self, pattern: str): ...
def mkdir(self, mode: int = 0o777, parents: bool = False, exist_ok: bool = False) -> None: ...
def rmdir(self) -> None: ...
# File operations
def open(self, mode: str = 'r', buffering: int = -1, encoding=None,
errors=None, newline=None) -> IO: ...
def read_text(self, encoding=None, errors=None) -> str: ...
def read_bytes(self) -> bytes: ...
def write_text(self, data: str, encoding=None, errors=None, newline=None) -> int: ...
def write_bytes(self, data: bytes) -> int: ...
def unlink(self, missing_ok: bool = False) -> None: ...
def rename(self, target) -> Path: ...
def replace(self, target) -> Path: ...
def symlink_to(self, target, target_is_directory: bool = False) -> None: ...
def hardlink_to(self, target) -> None: ...
def touch(self, mode: int = 0o666, exist_ok: bool = True) -> None: ...
# Class methods
@classmethod
def cwd(cls) -> Path: ...
@classmethod
def home(cls) -> Path: ...
class PurePath:
"""Pure path operations (no filesystem access)."""
# Similar interface to Path but without filesystem operations
class WindowsPath(Path):
"""Concrete path for Windows systems."""
class PosixPath(Path):
"""Concrete path for POSIX systems."""# File operations
def copy(src: str, dst: str) -> str: ...
def copy2(src: str, dst: str) -> str: ...
def copyfile(src: str, dst: str) -> str: ...
def copystat(src: str, dst: str) -> None: ...
def move(src: str, dst: str) -> str: ...
# Directory operations
def copytree(src: str, dst: str, symlinks: bool = False,
ignore=None, copy_function=copy2,
ignore_dangling_symlinks: bool = False,
dirs_exist_ok: bool = False) -> str: ...
def rmtree(path: str, ignore_errors: bool = False, onerror=None) -> None: ...
# Archive operations
def make_archive(base_name: str, format: str, root_dir=None,
base_dir=None, verbose: bool = False,
dry_run: bool = False, owner=None, group=None,
logger=None) -> str: ...
def unpack_archive(filename: str, extract_dir=None, format=None) -> None: ...
def get_archive_formats() -> list: ...
def get_unpack_formats() -> list: ...
# Disk usage
def disk_usage(path: str) -> tuple: ... # Returns (total, used, free)
# Which (find executable)
def which(cmd: str, mode: int = os.F_OK | os.X_OK, path=None) -> str: ...def glob(pathname: str, recursive: bool = False) -> list: ...
def iglob(pathname: str, recursive: bool = False): ...
def escape(pathname: str) -> str: ...def TemporaryFile(mode: str = 'w+b', buffering: int = -1, encoding=None,
newline=None, suffix=None, prefix=None, dir=None) -> IO: ...
def NamedTemporaryFile(mode: str = 'w+b', buffering: int = -1, encoding=None,
newline=None, suffix=None, prefix=None,
dir=None, delete: bool = True) -> IO: ...
def SpooledTemporaryFile(max_size: int = 0, mode: str = 'w+b',
buffering: int = -1, encoding=None,
newline=None, suffix=None, prefix=None,
dir=None) -> IO: ...
def TemporaryDirectory(suffix=None, prefix=None, dir=None) -> str: ...
def mkstemp(suffix=None, prefix=None, dir=None, text: bool = False) -> tuple: ...
def mkdtemp(suffix=None, prefix=None, dir=None) -> str: ...
def gettempdir() -> str: ...
def gettempdirb() -> bytes: ...
def gettempprefix() -> str: ...
def gettempprefixb() -> bytes: ...class stat_result:
"""Result of os.stat() call."""
st_mode: int # File mode
st_ino: int # Inode number
st_dev: int # Device
st_nlink: int # Number of hard links
st_uid: int # User ID
st_gid: int # Group ID
st_size: int # Size in bytes
st_atime: float # Access time
st_mtime: float # Modification time
st_ctime: float # Creation time (Windows) / metadata change time (Unix)# Named tuple factory creates classes like:
class NamedTupleClass(tuple):
"""Example named tuple class created by namedtuple()."""
def __new__(cls, field1, field2, ...): ...
@property
def field1(self): ...
@property
def field2(self): ...
def _asdict(self) -> dict: ...
def _replace(self, **kwargs): ...
@classmethod
def _make(cls, iterable): ...
_fields: tuple # Field names
_field_defaults: dict # Default values# Pattern flags (can be combined with |)
IGNORECASE: int # Case insensitive
MULTILINE: int # ^ and $ match line boundaries
DOTALL: int # . matches newlines including \n
VERBOSE: int # Allow verbose regular expressions
ASCII: int # ASCII-only matching
LOCALE: int # Locale-dependent matching