How to get terminal width?

Donn Cave donn at u.washington.edu
Fri May 5 23:50:26 CEST 2000


Quoth garabik at melkor.dnp.fmph.uniba.sk.spam (Radovan Garabik):
| Doug Stanfield <DOUGS at oceanic.com> wrote:
|  : Assuming you're on a *nix platform, there is an environmental variable,
|  : COLUMNS, that should be accessible.  If you use curses there is a

| hmmm...
|
| atlas13:pts/7:~% echo $COLUMNS
| 84

| atlas13:pts/7:~% python
| Python 1.5.2 (#0, Mar 12 2000, 22:39:11)  [GCC 2.95.2 20000220 (Debian GNU/Linux)] on linux2
| Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
| >>> import os
| >>> os.environ['COLUMNS']
| Traceback (innermost last):
|   File "<stdin>", line 1, in ?
|   File "/usr/lib/python1.5/UserDict.py", line 14, in __getitem__
|     def __getitem__(self, key): return self.data[key]
| KeyError: COLUMNS
|
| what's up?

That looks like a shell variable, as opposed to an environment variable.
Try "printenv COLUMNS", if you want to check that - echo $COLUMNS will
show either, so you can't tell, but printenv COLUMNS will show only an
environment variable.  Shell variables are known only to the shell process
itself, so Python can't see them.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list