w3resource

Python: Get height and the width of console window

Python Basic: Exercise-56 with Solution

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

Sample Solution:

Python Code:

# Import necessary modules for working with the terminal size.
import fcntl
import termios
import struct

# Define a function named 'terminal_size' to retrieve the terminal size.
def terminal_size():
    # Use 'fcntl' and 'termios' to fetch terminal-related information, including size.
    # This code queries the terminal's width (columns) and height (rows).
    # It uses the 'TIOCGWINSZ' ioctl command to get window size information.

    # 1. Open file descriptor 0 (stdin).
    # 2. Call 'fcntl.ioctl' with 'TIOCGWINSZ' to fetch window size information.
    # 3. Unpack the received information into 'th' (height), 'tw' (width), 'hp' (horizontal pixel size), and 'wp' (vertical pixel size).
    th, tw, hp, wp = struct.unpack('HHHH', fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH', 0, 0, 0, 0)))

    # Return the terminal width and height.
    return tw, th

# Call the 'terminal_size' function to get the terminal size and print the result.
print('Number of columns and Rows: ', terminal_size())

Sample Output:

Number of columns and Rows:  (110, 21)

Explanation:

The above Python code defines a function terminal_size() that uses system calls to get the size of the terminal window in columns and rows. It then calls the terminal_size() function and prints the result to the console output.

Then the code imports fcntl, termios, and struct modules.

fcntl - This module performs file control and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines.

termios - This module provides an interface to the POSIX calls for tty I/O control. For a complete description of these calls, see termios(3) Unix manual page.

struct - This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python values.

The terminal_size() function uses the fcntl.ioctl() function to query the size of the terminal window using the termios.TIOCGWINSZ ioctl call. This call returns a struct.pack() object containing four integers representing the number of rows and columns of the terminal, as well as the horizontal and vertical pixel size.

The struct.unpack() function is used to unpack the four integers from the struct.pack() object into separate variables th, tw, hp, and wp. The function then returns the values of tw (the width of the terminal window in columns) and th (the height of the terminal window in rows) as a tuple.

Finally, the print() function is used to print the size of the terminal window to the console.

Flowchart:

Flowchart: Get height and the width of console window.

Python Code Editor:

 

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/python-exercises/python-basic-exercise-56.php