Getting The Terminal Size In Python

Many times our output doesn’t get fit into the terminal of default size. In that case, we have to pull it accordingly and make the size as per our need.


image: Akshay Chauhan on Unsplash

In this article, I’ll show you those few lines of Python code using which you can get the size of your terminal programmatically.

Now, before resizing our output we need to know, how to read the terminal size. There are possibly many ways to get the terminal size, i.e. reading environment variables, making low level system calls, etc.

In this article, I’ll show you one of the easiest and simplest way to get the terminal size.

import os
s = os.get_terminal_size()
print(s.columns, s.lines)

You can also watch the video recording of this article on my YouTube channel here.