Python 3.3+shutilfunction
shutil.get_terminal_size()
system and environment
Returns the terminal window size (columns, lines), honouring the COLUMNS and LINES variables, with an 80x24 fallback.
Syntax
shutil.get_terminal_size(fallback=(80, 24)) -> os.terminal_size
Prefer it to os.get_terminal_size: that one raises OSError when output goes to a file or a pipe, while this function returns the fallback ((80, 24) by default). The COLUMNS and LINES environment variables are checked first — so a user or a test can set the width explicitly. The size is read at call time; if the window is resized, call it again.
Examples
import shutil
size = shutil.get_terminal_size()
print(size.columns, size.lines) # → ? 80 24 — зависит от терминала
import os, shutil
os.environ['COLUMNS'] = '100'
os.environ['LINES'] = '30'
print(shutil.get_terminal_size()) # → os.terminal_size(columns=100, lines=30)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy