[Tutor] Clear screen questions

Steven D'Aprano steve at pearwood.info
Mon May 6 02:54:15 CEST 2013


On 06/05/13 10:17, boB Stepp wrote:
> I have been playing around with my own version of a guess the number
> game that I wrote before looking at how the kids' book (which I am
> reviewing for my kids) did it. For some reason my children are
> fascinated by this game and keep asking me for improvements. The
> latest one was to implement an automatic clearing of the screen if the
> user wishes to play again. So I got my handy-dandy pocket reference
> out and added to the existing code at the appropriate places:
>
> import os
> ...
> os.system('cls')
>
> The kids were pleased, but I wasn't. Yes, this works on Windows/DOS,
> but nowhere else. So how to make this OS-independent? Googling has not
> yielded a truly cross-platform solution. Currently, I have temporized
> with:
>
> os.system('cls' if os.name=='nt' else 'clear')
>
> which I think will work for Windows/UNIX. I am not certain it would
> work with DOS, since NT came after DOS, but who has DOS these days?

Certainly not Python. DOS is not supported. I think Windows 2000 is the oldest version still supported.

But even on Windows that will not work, if you are running under IDLE. And unfortunately there is no official way to tell if you are running under IDLE, or any other IDE for that matter.


> So my main question is there a truly clean, cross-platform solution to
> the clear screen dilemma? If my online searching is accurate, then the
> answer appears to be no, unless one wants to print many blank lines.

Your googling is accurate. There is no clean, cross-platform solution, not even for the "main three" (Linux/Unix, Windows, Mac), let alone minority and legacy platforms, other implementations, etc.


> A second question is that one person had as the answer to use:
>
> os.system( [ 'clear', 'cls' ][ os.name == 'nt' ] )
>
> I don't understand this syntax. The writer said that if one
> understands what this is doing, then the method is more generally
> useful. Would someone explain how this works? And hopefully it will
> become apparent to me how this is more generally useful?


You start with a call to os.system. What's the argument to os.system? Well, *eventually* is has to become a single string, but it can be an expression that evaluates to a string. What is this expression? It starts with a list of two strings:

['clear', 'cls']


which is then indexed by the result of another expression, os.name == 'nt'. Under Windows, this evaluates to True, otherwise to False:

['clear', 'cls'][True]  # under Windows
['clear', 'cls'][False]  # everything else


A little known fact: True evaluates as equal to 1, and False as equal to 0. (This is mostly for historical reasons, but it's also useful.) So the above ends up as:

['clear', 'cls'][True]  # under Windows
=> 'cls'

['clear', 'cls'][False]  # everything else
=> 'clear'


and finally os.system gets to run a single string.



> Finally, a minor, secondary question: I was curious how my clear
> screen code would run in the IDLE Python shell window, since I know
> there is currently no way to clear its screen (other than the blank
> line approach). Of course it did not clear the screen, but it did do
> something unexpected that I would like to understand. Upon hitting the
> clear screen code the active window switched from the Python shell
> window to the Python edit window. Why is this so?

Magic. IDLE bug. Who knows? It doesn't do that for me, but I've only tested it interactively with one version on Linux.



-- 
Steven


More information about the Tutor mailing list