1

I would like to write a python script for use on Windows and Linux that clears the screen.
Most of the examples for this are something like:

import os
os.system('cls')

which works, but is a bit dicey given all of the issues with making system calls (security issues). Is there a better way of clearing the terminal in python without needing to use system?
The best alternative I have found so far was this:

print("\033c");

but it has the slight annoyance of removing everything from the terminal
(ie I would like it to clear the terminal, but the user should be able to scroll up and see previous output in the terminal).

1
  • Print multiple newlines, print "\n"*20 Commented Aug 26, 2015 at 20:53

3 Answers 3

2

The following ANSI escape code should help on linux (and most *nix unless you find a really weird terminal):

print("\x1b[2J\x1b[H",end="")

It'll clear the screen and put your cursor at the top left. You can still scroll up to find your old stuff but you may have to go up a decent distance to find it.

I have absolutely no idea what it'll do on windows. You may find you need to detect the os and use a different method there.

For python 2.x you'll need to use sys.stdout.write instead of the print statement as you can't suppress the \n on print in 2.x as far as I know.

Sign up to request clarification or add additional context in comments.

5 Comments

how exactly do I use it properly in 2.x? I tried >>> import sys >>> sys.stdout.write("\x1b[2J\x1b[H",end="") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: write() takes no keyword arguments
@BruceJohnJennerLawso, don't use end="" with sys, you can also from __future__ import print_function
@BruceJohnJennerLawso just use sys.stdout.write("\x1b[2J\x1b[H") its only print that sticks the '\n' on the end by default. .write doesn't add anything.
@BruceJohnJennerLawso: Look at the sequence elements here. The "\x1b[" tells it that an ANSI code is coming up. This happens twice in that string. The first is just before "2J". "J" tells it to clear the screen and "2" tells it to clear the whole screen rather than just the bit between the cursor and the beginning. The second "\x1b[" is followed by the letter "H". This tells it to move the cursor. You can add numbers before the "H" to tell it where to move but the default is the top left so we don't need any.
Any example of “weird terminal”?
0

If you have special knowledge of the screen size you can use a modified version of your original print-based answer.

def cls(x):
    """Clears the screen after printing x newlines."""
    print "\n" * x
    print "\033c"

In Python 3.3 and later you can divine the size of the Terminal window with shutil, but I don't think there's a great way to do it in 2.7 without actually importing os, which you said should be avoided.

2 Comments

that does work, but I wanted the prompt to be at the top of the screen afterwards as well, I should have mentioned that.
Ok, edited. I was half-joking, but I guess there's nothing funny about 65k spaces
0

This piece of code doesn't call os directly from the code.
Try this:

from subprocess import call
def clear(int=None):  
    call('clear')
    if int == 0:
       exit()
clear()

It worked for me, I work on linux but I think it will work on windows to.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.