Python | shutil.get_terminal_size() method

Shutil module in Python provides many functions of high-level operations on files and collections of files. It comes under Python’s standard utility modules. This module helps in automating process of copying and removal of files and directories.
shutil.get_terminal_size() method tells the size of the terminal window. As terminal is of the two dimensions, so the environment variables COLUMNS and LINES respectively is checked. If the variable is defined and the value is a positive integer then it is used. When COLUMNS or LINES is not defined then the terminal connected to sys.__stdout__ is queried by invoking os.get_terminal_size().

Syntax: shutil.get_terminal_size(fallback=(columns, lines))

Parameters:
fallback: A tuple representing the columns and lines of terminal. The default value of fallback used by many terminal emulators is (80, 24).



Return Value: This method returns a named tuple of type os.terminal_size.

Example #1 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain
# shutil.get_terminal_size() method 
  
# importing shutil module 
import shutil 
  
# Using shutil.get_terminal_size() method 
terminal_size = shutil.get_terminal_size()
  
# Print result 
print(terminal_size) 

chevron_right


Output:

os.terminal_size(columns=80, lines=24)

Example #2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to explain
# shutil.get_terminal_size() method 
  
# importing shutil module 
import shutil 
  
# Using shutil.get_terminal_size() method 
terminal_size = shutil.get_terminal_size((40, 20))
  
# Print result 
print(terminal_size) 

chevron_right


Output:

os.terminal_size(columns=40, lines=20)


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.