[Tutor] How to clear the screen

Alan Gauld alan.gauld at btinternet.com
Mon Oct 20 10:10:43 CEST 2008

"Johnny" <jgowen at lorettotel.net> wrote

> In this program, it would be really neat if their was a way to clear 
> the screen off.
> I have inserrted comments as to where I'd like the screen to be 
> cleared.

Such a simple question. Such a complex answer :-)

It all depends on what OS and terminal setup you are using.

If its windows you can do several things:
1) call the DOS CLS command using os.system() or similar
2) send the ANSI clear screen control code to the DOS window
   (if ANSI.SYS is installed)
3) send a lot of \ns to the screeen (simplest and most portable but 
4) Use a  3rd party screen handling module

If its Linux/MacOS/*nix:
You can use similar but slightly different tricks
1) call the Unix clear command
2) Send the clear screen control codes as determined by termcap
3) Send a lot of \n to screen
4) use the curses module to control the screen

Personally I'd go for 3 and write something like:

def cls(n=50): print "\n" * n


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list