How to get Linux console window width in Python
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When a Python program formats output for a terminal, it often needs to know how many columns are currently available. The simplest and most portable answer on modern Python is shutil.get_terminal_size(), with fallback handling for cases where the program is not attached to a real terminal.
The Recommended Standard-Library Approach
This returns a os.terminal_size object with columns and lines. The fallback matters because many programs run in environments where standard output is redirected or where no interactive terminal exists.
os.get_terminal_size Works Too
If you want the lower-level call directly, use os.get_terminal_size.
This is fine when you know the file descriptor belongs to a terminal. If it does not, Python raises OSError.
Handle Non-TTY Environments Explicitly
Programs do not always run in an interactive shell. They may run under cron, CI, pipes, or output redirection.
That pattern avoids exceptions and gives your application a predictable fallback width.
Why Fallback Behavior Matters
If your code assumes a terminal always exists, it can crash when someone pipes output to a file.
In that situation, stdout is no longer an interactive terminal device. Your formatting code should degrade gracefully instead of failing just because it cannot measure a screen width.
Environment Variables Are Not The Best Primary Source
Some terminal-aware tools export COLUMNS and LINES, but relying on those variables directly is less robust than using the standard library call, because they may be missing or stale.
They can still be useful as a fallback or debugging clue, but not usually as your first choice.
Why stty size Is Usually Unnecessary
You can fetch terminal size through subprocess calls such as stty size, but that is usually slower and less reliable than the standard library.
This works in some shells, but it depends on external commands and can fail in containerized or non-interactive environments. Prefer shutil.get_terminal_size() unless you have a specific reason not to.
Wrapping Output Based On Width
Once you have the width, you can format output accordingly.
This is a simple example, but the same value can drive progress bars, tables, and TUI layouts.
Linux-Specific Versus Portable Code
The question often mentions Linux, but the standard-library approaches above are not Linux-only. That is usually a good thing. Unless you truly need Linux-specific terminal ioctls, portable code is easier to test and maintain.
Common Pitfalls
The most common mistake is calling os.get_terminal_size() in a non-terminal environment and not handling OSError. Another is assuming redirected output still has a console width in the interactive sense. Developers also sometimes use stty or environment variables as the primary solution even though the standard library already handles the common cases better. Finally, if your application caches the width forever, it may ignore terminal resize events during long-running sessions.
Summary
- '
shutil.get_terminal_size(fallback=(80, 24))is the simplest recommended solution.' - '
os.get_terminal_sizeworks when you know you have a real terminal file descriptor.' - Always consider non-interactive environments such as pipes, cron, and CI.
- Avoid depending on external commands like
sttyunless necessary. - Use a fallback width so console formatting remains stable even without a TTY.
Related reading
- How to get list of points inside a polygon in python?
- How to get MD5 sum of a string using python?
- How to get only the last part of a path in Python?
- How to get PI in tensorflow?
- How to get pipenv running in docker?
- How to get POSTed JSON in Flask?
- How to get precision, recall and f-measure from confusion matrix in Python
- How to get Python requests to trust a self signed SSL certificate?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.