dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

How do I clear the Console Screen

Sun Apr 21, 2013 2:38 pm

Hi, I would like to clear the terminal window to display sensor readouts.

I used to use console.clear on VB.

dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 2:42 pm

Sorry
it's

Code: Select all

import os

os.system('clear')
from http://stackoverflow.com/questions/1432 ... thon-shell

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 2:51 pm

Your method however doesn't actually clear the console screen, it just pushes everything out of sight. try this for comparision:

Code: Select all

os.system('printf "\033c"')
or simply (python3 example)

Code: Select all

print ("\033c")

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 3:12 pm

PS1981 wrote:Your method however doesn't actually clear the console screen, it just pushes everything out of sight.
Do you have the correct TERM setting?

"clear" should send "\033[H\033[2J", which homes the cursor and then clears the whole screen. "\033c" is a full reset, which is slower and more comprehensive, but should not normally look different.

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 3:17 pm

Don't know to be honest. Just works for me as described.

User avatar
SN
Posts: 1014
Joined: Mon Feb 13, 2012 8:06 pm
Location: Romiley, UK
Contact: Website

Re: How do I clear the Console Screen

Sun Apr 21, 2013 3:35 pm

oh the wonders of ANSI escape codes.... takes me straight back to 1990

Full list here, out of interest
http://en.wikipedia.org/wiki/ANSI_escape_code
Steve N – binatone mk4->intellivision->zx81->spectrum->cbm64->cpc6128->520stfm->pc->raspi ?

PS1981
Posts: 445
Joined: Wed Dec 12, 2012 4:11 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 3:40 pm

SN wrote:oh the wonders of ANSI escape codes.... takes me straight back to 1990
when I were a lad... :D

I was just trying some stuff out anyway, it appears (to me at least) that if you are really in the console "clear" does what is required. The use of "clear" however when in a terminal window of the x desktop leaves the option of scrolling up with the scroll bar at the side. It depends what the OP is up to. If clear is somehow quicker or uses less resources and is at the same time adequate, then that's fine.

dawson345
Posts: 16
Joined: Mon Mar 11, 2013 12:47 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 4:51 pm

Thanks guys :)

User avatar
jojopi
Posts: 3233
Joined: Tue Oct 11, 2011 8:38 pm

Re: How do I clear the Console Screen

Sun Apr 21, 2013 5:38 pm

On the console you can still scroll up after both clear and reset. There is no scroll bar of course, but the keystroke is the same: Shift+PageUp. Personally I would find it mildly irritating if a program cleared the scrollback without a good reason.

I was mistaken about the speed difference, though. The noticeable pause when running "reset" is a feature of that program rather than the escape sequence itself.

bcbrown
Posts: 3
Joined: Sun Apr 02, 2017 2:54 pm

Re: How do I clear the Console Screen

Wed Apr 05, 2017 8:40 pm

When u use this it prints a random Integer. Any comments?

Return to “Python”