Simply press ctrl + L
(works on the shell too - you don’t even need to write clear
).
This in the terminal. In a program, you can simply print a number of blank lines larger than the terminal size: print("\n" * 130)
, or, if you’re on Linux or Mac, print the ANSI sequence that erases the screen:
print("\x1b[2J")
. See more details below.
CTRL+L clears the screen if you’re on the terminal of some Linux - and maybe one or the other of the interactive Windows environments - but not in Idle, and other Python prompts integrated into the IDE’s - and it’s certainly not appropriate if you want to delete the screen from within a program (You cannot ask the user to type "Ctrl+L" to erase the screen :-) )
The reason there is no simple function call,in the standard Python library is that standard inputs and outputs are not thought of, in a generic programming language, always as a "terminal" - they represent an input data stream, and two output (stdout and stderr). But the language itself "doesn’t know" that is running on the terminal when some program runs. For programs specifically made for the terminal that want a more sophisticated interaction (including with the mouse), the standard Python library includes the module curses
(see below).
The solution calling the external command - be the cls
in windows or clear
on Linux/Mac OS, with os.system
works -but has a basic problem: it is an external program - a whole external process in the operating system, find the file on disk, create the process, to make an API call to the terminal. In terms of resource uses is the same as calling a key chain to open and lock the door again every time you leave the house.
Well - these programs to "erase the screen" - both the Unix clear and the DOS "cls", as the "clear" and the CTRL + L itself, do not do much more than simply print several blank lines, so that the contents of the current screen scroll up.
To do this in Python, simply print a single string with a sufficient number of "linefeeds" (the character represented by ' n' - whose decimal code is "10").
A little more boring is knowing the exact number of linefeeds needed to erase the screen exactly and without too many linefeeds. If that’s not a concern, a number like 130 blank lines is more than enough to roll a full HD terminal with size 4 font - so simply:
print ("\n" * 130)
Guaranteed to erase any terminal, regardless of the type of S.O. without having to execute another process outside the Python interpreter just for that.
Of course, it is more elegant to print a number of blank lines only equal to the number of terminal lines - and no more than that. Among other things, a user accustomed to using the terminal can expect to be able to scroll the screen up to see the previous screen in the lines immediately preceding the first line of the blank screen (although the command clear
Restore this historical too).
For this, from Python 3.3, there is the function get_terminal_size
in the Python OS module. For those who are writing a program that will work in several versions of Python, the functionality can be encapsulated in a function like:
def clear():
try:
import os
lines = os.get_terminal_size().lines
except AttributeError:
lines = 130
print("\n" * lines)
But in Python 3.3 or above, just do:
import os
print("\n" * os.get_terminal_size().lines)
In addition, in Unixes, and perhaps in some Windows terminal/prompt programs, the "sequences ANSI" - that is, the terminal itself recognizes special sequences of characters that represent commands such as cleaning the screen, changing the font color, positioning the cursor -etc - are a very interesting "toy" - and much simpler than the module curses
Python (of which I speak below). To erase the screen on any Linux terminal or most Unixes (I don’t know if Mac OS X works directly) - just print the sequence " x1b[2J" - (the " x1b" is the character <ESC>
the same code that is generated by the key with that name). The sequence <ESC>[
starts several ANSI sequences. To see the exact sequences supported by your terminal, run the command infocmp
in the shell.
By this technique, deleting the screen, and positioning the cursor in the first row and first column can be done like this:
print("\x1b[2J\x1b[1;1H")
(The print command prints by default an " n" - if in Python 3 use print("\x1b[2J\x1b[1;1H", end="")
to avoid stopping at the second line - in Python2, it is worth putting a from __future__ import print_function
at the beginning of the file and do the same.)
Remembering that - and from what I understood from the context of this question - if the idea is just to delete the screen while in interactive Python mode - in this case, the "Ctrl + L" will serve well.
For those who want to make a complex program with interaction by the terminal, my tip is always the following: if you are at this point, it is time to think about escaping from the terminal. Python allows interaction with various graphic libraries to create an application in a window of its own - which is much more comfortable for the end user. Users accustomed to command line interface will be happy to call the program from the system command line, passing options directly in the same - for this see the module argparse of the standard library.
If still the author prefers to create an interactive interface in the terminal, the tip is to use the module curses - also from the standard library - with it you get an object of type "window" (text) in the terminal, where you can position the cursor in exact places, erase the screen, change colors, etc... And with some work (not little) you can make an app with a really professional face - since the users of this program have been frozen in time since the 1990s. If that’s not the point, delete the screen with the print("\n" * 130)
is enough.
With curses, you can erase the screen by doing:
import curses
window = curses.initscr()
window.clear()
window.refresh()
However the terminal will be in this "application" mode where "print" and "input" may have different results than in a normal terminal. In particular, it is important to position the cursor in the desired position of the screen before each print calling the function window.move(line, col)
. Upon terminating the program, it is vital to return the terminal to the normal state by calling the function curses.endwin()
.
The author wants python to clean the screen, could edit his answer?
– Guilherme Nascimento
No - the author asks how to clean the console in the interactive Python terminal - and this is the most practical way. But yes, the answer is short, - and I can put later on how to make Python clean the screen. Note that neither of the other two answers does this however: they call external programs uqe wipe the screen (so it’s not "python").
– jsbueno
Precisely the other answers are programs, because he is probably writing a program in python that works on the terminal and probably he wants in a certain action of the program to clean the screen and this seems to me the reason why the other answers are python codes. Perhaps the author of the question was unable to convey the need and this would make his answer valid yes. However in these cases, it is worth more you comment on the question which requesting details. Please also take into account the scope: http://answall.com/help/on-topic
– Guilherme Nascimento
In my reading of the question there are two tips that the author’s problem is not to write a porgram - but to delete the console in interactive mode. But I agree it’s all right.
– jsbueno