[Tutor] Clearing the Console Screen

Michael P. Reilly arcege at gmail.com
Thu Jun 16 18:38:29 CEST 2005

On 6/16/05, Don Parris <webdev at matheteuo.org> wrote:
> Thanks! I thought there had to be a way to call the OS' clear screen
> command, but was going about it the wrong way. I was trying to use
> sys.clear instead of os.system. Would it be difficult to test the OS,
> store the result in a variable, and call the comand based on the variable
> result? Or would it be simpler to have users edit the script for their OS?
> Mind you, there may be other areas where I need an OS-specific command. 
> I'm
> beginning to get an idea of the challenges of portability though. ;)
> Don

You might want to use the tput command. It returns control sequences for the 
terminal that are used by all programs, e.g. emacs, vi(m). Your could 
capture the output with the commands module and send it to the terminal with 
stdout.write (instead of print).

import commands

clear_str = None
def clearscreen():
global clear_str
from sys import stdout
if not clear_str:
clear_str = commands.getoutput('tput clear')


If you wanted, you could encapsulate this into a module or a class. This 
would work on just about any UNIX/Linux/BSD system, and might work on Mac 
OS/X (I don't know if they use terminfo).

You can use the other tput subcommands in your program if you wished as 
well. (Hide the cursor, go to column X row Y, etc.)
There's so many different worlds,
So many different suns.
And we have just one world,
But we live in different ones.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20050616/0a9b4d2e/attachment.htm

More information about the Tutor mailing list