How to get Linux console window width in Python
To swiftly fetch the console width in a Python script, employ the shutil.get_terminal_size()
function from the shutil
module or use the os.get_terminal_size()
function from the built-in os
library. Both methods return a tuple where the width (columns) of the console is the first element as shown below:
The above code snippet conveniently prints the width of your console.
Role in Text-based Interfaces
The console width is instrumental in devising text-based interfaces. The benefits multiply with the prevention of line wrapping achieved with the columns
attribute obtained from shutil.get_terminal_size()
.
Piping and Redirection: A Special Case
When you've got piping or output redirection in play, it's safe to bet on os.get_terminal_size(0)
. By doing so, the real terminal dimensions are fetched even if your output is not interacting directly with it.
Digging Deeper: Fallbacks and Lower-Level APIs
Fallback Strategy
To effectively handle situations where terminal size retrieval might fail, adopt the fallback technique. Provide a default size to the get_terminal_size(fallback=(80, 20))
function for a seamless operation, even in the face of adversity.
Low-level ioctl System Call
If you dare to delve into the deeper Linux-specific realms, you could use the fcntl.ioctl
system calls. Invoke these calls with TIOCGWINSZ
to get into the low-level mechanisms for extracting the window size:
Platform and Version Compatibility
These methods are cross-platform and run on Linux, Mac OS, and Windows. For Pythonistas using versions under 3.3, get_terminal_size()
has got your back with a backport.
A Ride into Alternatives
Linux-specific 'stty size' Command
For those who are partial to Linux, calling stty size
would do the trick:
For this one, remember to proceed with caution, as this might not deliver in a non-interactive shell.
When not to use os.environ["COLUMNS"]
You may come across scripts that use os.environ["COLUMNS"]
; however, this variable can be misleading as it may not represent the current console width, particularly if the console is resized post environment variables initialization.
Comprehensive Tips, Tricks, and Use Cases
Handling Practical Scenarios
Being aware of your terminal's width is key to formatting output, creating dynamic tables, and designing CLI tools. Here's how you use the width in a practical case:
Width Versus Pixel Dimensions
Ensure that you're clear about what you're dealing with. The terminal width pertains to the number of characters per line, not pixel dimensions. This understanding is critical for text-oriented applications.
More Than a Width
Digging deeper into terminal interactions would lead you to the curses
library in Python. This module helps create text-based user interfaces in a more sophisticated manner.
Was this article helpful?