On this page
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
- POSIX-only: Not available on Windows (use
msvcrtor Win32 Console APIs on Windows). - Fails with
termios.erroriffdis not a real TTY (e.g., piped stdin or CI runners): Guard withos.isatty(fd). - Always restore
old_attrsinfinally:: Leaving a terminal in raw/no-echo mode breaks the user's shell after your program exits.
Common Functions
- termios.tcdrain() —
tcdrain(fd, /)— Wait until all output written to file descriptor fd has been transmitted. - termios.tcflow() —
tcflow(fd, action, /)— Suspend or resume input or output on file descriptor fd. - termios.tcflush() —
tcflush(fd, queue, /)— Discard queued data on file descriptor fd. - termios.tcgetattr() —
tcgetattr(fd, /)— Get the tty attributes for file descriptor fd. - termios.tcgetwinsize() —
tcgetwinsize(fd, /)— Get the tty winsize for file descriptor fd. - termios.tcsendbreak() —
tcsendbreak(fd, duration, /)— Send a break on file descriptor fd. - termios.tcsetattr() —
tcsetattr(fd, when, attributes, /)— Set the tty attributes for file descriptor fd. - termios.tcsetwinsize() —
tcsetwinsize(fd, winsize, /)— Set the tty winsize for file descriptor fd.
Classes & Objects
- termios.error —
termios.errorclass. (Methods:.add_note(),.with_traceback())
Constants & Attributes
termios.ALTWERASE(int) =512termios.B0(int) =0termios.B110(int) =110termios.B115200(int) =115200termios.B1200(int) =1200termios.B134(int) =134termios.B14400(int) =14400termios.B150(int) =150termios.B1800(int) =1800termios.B19200(int) =19200termios.B200(int) =200termios.B230400(int) =230400termios.B2400(int) =2400termios.B28800(int) =28800termios.B300(int) =300
Linked from
- tuts/python3/stdlib_module
- tuts/python3/stdlib_module/curses
- tuts/python3/stdlib_module/stdlib_module
- tuts/python3/stdlib_module/tempfile
- tuts/python3/stdlib_module/termios/error
- tuts/python3/stdlib_module/termios/tcdrain
- tuts/python3/stdlib_module/termios/tcflow
- tuts/python3/stdlib_module/termios/tcflush
- tuts/python3/stdlib_module/termios/tcgetattr
- tuts/python3/stdlib_module/termios/tcgetwinsize
- tuts/python3/stdlib_module/termios/tcsendbreak
- tuts/python3/stdlib_module/termios/tcsetattr
- tuts/python3/stdlib_module/termios/tcsetwinsize
- tuts/python3/stdlib_module/textwrap
- tuts/python3/stdlib_module/tty
Related notes
- tuts/python3/stdlib_module/stdlib_module Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/tempfile Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/error Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcdrain Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcflow Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcflush Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcgetattr Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcgetwinsize Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcsendbreak Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/termios/tcsetattr Linked here; links to this page; shared tags: #stdlib