[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

get the terminal's size

That will tell you the terminal size at the time Python was started.

If the terminal size has changed while Python was running, those

environment variables will be wrong.  You need to use the TIOCGWINSZ

ioctl call:


And to detect the size changes (so you know _when_ you need to do the

above), you need to attach a signal handler for the WINCH signal.


I'm running a python 3 interpreter on linux. I'm actually ssh'd into the

on a headless server. And so my terminal is my local laptop terminal
window, with

the python interpreter running on the remote linux box terminal,

over an ssh connection.

$ python3

Python 3.6.7 (default, Oct 22 2018, 11:32:17)

[GCC 8.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import shutil

>>> print(f"{shutil.get_terminal_size()}\n")

os.terminal_size(columns=118, lines=63)

>>> print(f"{shutil.get_terminal_size()}\n")

os.terminal_size(columns=133, lines=63)

>>> print(f"{shutil.get_terminal_size()}\n")

os.terminal_size(columns=118, lines=65)

>>> print(f"{shutil.get_terminal_size()}\n")

os.terminal_size(columns=118, lines=63)

With the python interpreter running on the remote terminal, I have resized

the terminal window on my local laptop several times. And each time, the

python interpreter knows about the change, correctly printing the new size.

have done nothing with environment variables. I have not used a signal

for the WINCH signal. It just works.