Why is there no platform independent way of clearing a terminal?

Phil Thompson phil at riverbankcomputing.com
Tue Jul 27 10:36:55 EDT 2010


On Tue, 27 Jul 2010 16:02:23 +0200, Bruno Desthuilliers
<bruno.42.desthuilliers at websiteburo.invalid> wrote:
> Daniel Fetchinson a écrit :
>> Hi folks,
>> 
>> If I'm only interested in linux and windows I know I can do
>> 
>> ################################
>> import os
>> import platform
>> 
>> if platform.system( ) == 'Linux':
>>     clear = 'clear'
>> else:
>>     clear = 'cls'
>> 
>> os.system( clear )
>> ################################
>> 
>> or something equivalent using os.name and friends, but was wondering
>> why there is no platform independent way (i.e. the platform dependence
>> is taken care of by the python stdlib) of clearing a terminal. Sure,
>> there are many different terminals and many different operating
>> systems but in many areas python managed to hide all these
>> complexities behind a well defined API.
>> 
>> Why was clearing a terminal left out?
>> 
> 
> What you're talking about is a shell, not a terminal (a terminal is a 
> physical device). And the shell is not necessarily part of the OS itself

> (there's no shortage of shells for unices / linux systems), so it 
> doesn't belong to the os or platform modules.
> 
> FWIW, I can't tell for sure since I never used any other shell than 
> bash, but I'm not sure your above code is garanteed to work on each and 
> any possible unix shell.

Sorry, but that is completely wrong - the shell is irrelevant.

"clear" is just a normal command line program that queries the
termcap/terminfo database (possibly via the curses library) for the
terminal specific sequence of characters that will clear the screen. It
then writes those characters to stdout. The terminal, or (more usually
these days) terminal emulator, then interprets those characters and takes
the appropriate action.

I'm not sure what the POSIX status of the clear command is, but I'd be
surprised if it wasn't present on a UNIX/Linux system of any vintage.

Phil




More information about the Python-list mailing list