On this page
  1. termios
  2. Import
  3. Overview
  4. Quick Start
  5. Implementation Walkthrough
  6. 1. Safe Save-and-Restore Context Manager for Raw / Cbreak Input
  7. Gotchas & Boundaries
  8. Common Functions
  9. Classes & Objects
  10. Constants & Attributes

termios

Prev: tempfile | Up: stdlib_module | Next: textwrap
Rel: os, io, time, argparse, getopt
Ref: docs.python.org — termios
Tags: #public #python3 #stdlib


Import

import termios

Overview

Low-level POSIX terminal I/O control (tcgetattr, tcsetattr, tcflush, TIOCGWINSZ/TIOCSWINSZ) for toggling echo, canonical line buffering, raw keystroke reads, and terminal window size. Always snapshot original attributes with tcgetattr() and restore them in a finally: block.

Quick Start

import termios

# Constants for inspecting/configuring POSIX TTY attributes:
assert hasattr(termios, "TCSADRAIN") and hasattr(termios, "ECHO")

Implementation Walkthrough

1. Safe Save-and-Restore Context Manager for Raw / Cbreak Input

from contextlib import contextmanager
import termios
import tty

@contextmanager
def raw_terminal(fd: int):
    old_attrs = termios.tcgetattr(fd)
    try:
        tty.setraw(fd)
        yield
    finally:
        termios.tcsetattr(fd, termios.TCSADRAIN, old_attrs)

Gotchas & Boundaries

Common Functions

Classes & Objects

Constants & Attributes

Linked from

Related notes