w3resource

Python: Get height and the width of console window

Python Basic: Exercise-56 with Solution

Write a Python program to get height and the width of console window.

Sample Solution:-

Python Code:

def terminal_size():
    import fcntl, termios, struct
    th, tw, hp, wp = struct.unpack('HHHH',
        fcntl.ioctl(0, termios.TIOCGWINSZ,
        struct.pack('HHHH', 0, 0, 0, 0)))
    return tw, th

print('Number of columns and Rows: ',terminal_size())

Sample Output:

Number of columns and Rows:  (110, 21)

Flowchart:

Flowchart: Get height and the width of console window.

Python Code Editor:

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python to find local IP addresses using Python's stdlib.
Next: Write a program to get execution time (in seconds) for a Python method.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Walrus Operator:

>>> a = ['j', 'a', 'k', 'd', 'c']
>>> if (n := len(a))%2 == 1:
...     print(f'The number of letters is {n}, which is odd.')
...
The number of letters is 5, which is odd.