158

Possible Duplicates:
clear terminal in python
How to clear python interpreter console?

I'm trying to write a program in Python but I don't know how to clear the screen. I use both Windows and Linux and I use commands to clear the screen in those, but I don't know how to do it in Python.

How do I use Python to clear the screen?

braX
  • 11,506
  • 5
  • 20
  • 33
shiester
  • 1,589
  • 2
  • 10
  • 3
  • duplicate of http://stackoverflow.com/questions/2084508 http://stackoverflow.com/questions/2466866 http://stackoverflow.com/questions/1432480 http://stackoverflow.com/questions/517970 – BlueRaja - Danny Pflughoeft Jan 26 '11 at 22:03
  • Already asked & answered a few times: http://stackoverflow.com/questions/4807925/cross-platform-way-of-clearing-the-screen-in-python, http://stackoverflow.com/questions/517970/how-to-clear-python-interpreter-console, http://stackoverflow.com/questions/2084508/clear-terminal-in-python – GreenMatt Jan 26 '11 at 22:04
  • 5
    Google "Python clear screen" gets HUNDREDS of hits. – Jim Garrison Jan 26 '11 at 22:04
  • 24
    Linux [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop): `Ctrl+L` – Brent Bradburn Sep 15 '13 at 18:53
  • 61
    Jim Garrison: 2 years later, this is the first Google result. Poetic injustice... – Lenoxus Apr 03 '14 at 18:51
  • There is now a simple class: pip3 install clear-screen and then to use it: from clear-screen import clear and then the method clear() will clear your shell –  Jan 27 '19 at 23:04

1 Answers1

299

If you mean the screen where you have that interpreter prompt >>> you can do CTRL+L on Bash shell can help. Windows does not have equivalent. You can do

import os
os.system('cls')  # on windows

or

os.system('clear')  # on linux / os x
jesterjunk
  • 2,342
  • 22
  • 18
Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
  • 25
    To avoid the 0 (`system`'s return value) being shown, use `absolutely_unused_variable = os.system("cls")` or `absolutely_unused_variable = os.system("clear")` – Shravan Jul 04 '15 at 08:01
  • 32
    `_=os.system("cls")` or `_=os.system("clear")` seems much better – kien_coi_1997 Jan 08 '16 at 11:09
  • 30
    This answer is as close as the absolute way of not doing it as one can get. You don't need to run an entire outside process just to clear the screen - just print a number of newlines higher than the screen height: `print ("\n" * 100) ` – jsbueno Jun 02 '16 at 05:21
  • 3
    @jsbueno Please **please** post as answer. Please. Then tell me, so I can +1 it. – wizzwizz4 Jul 20 '16 at 12:51
  • 1
    This question is closed as duplicate of the other - it can't accept new answers. – jsbueno Jul 20 '16 at 15:55
  • 52
    just adding hundrets of new lines is stupid because the promt is then at the bottom of the screen not at the top which is not really clearing the console but spamming things that are invisible – Alon May 18 '17 at 07:57
  • Is there a way to dynamically pass the string-argument so that the code is independent of the OS? – MahNas92 Apr 12 '19 at 12:24
  • clear = os.system("cls") and no output 0 – Cornea Valentin Jan 19 '20 at 00:56
  • 7
    `cls = lambda: print("\033c\033[3J", end='')` and then `cls()`, or directly `print("\033c\033[3J", end='')` – Mario Palumbo Dec 17 '20 at 15:47