3

How can I in Python (v 2.7.2) completely clear the terminal window?

This doesn't work since it just adds a bunch of new lines and the user can still scroll up and see earlier commands

import os
os.system('clear')
braX
  • 11,506
  • 5
  • 20
  • 33
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
  • possible duplicate of [How to clear python interpreter console?](http://stackoverflow.com/questions/517970/how-to-clear-python-interpreter-console) – Games Brainiac Oct 14 '13 at 11:42

8 Answers8

4

Found here that the correct command to use was tput reset

import os

clear = lambda : os.system('tput reset')
clear()
Community
  • 1
  • 1
Oskar Persson
  • 6,605
  • 15
  • 63
  • 124
0

I've tried this and it works:

>>> import os
>>> clear = lambda : os.system('clear')
>>> clear()

BEFORE

Before

AFTER clear()

After

Games Brainiac
  • 80,178
  • 33
  • 141
  • 199
0

On Windows:

import os
os.system('cls')

On Linux:

import os
os.system('clear')
Adrien
  • 1
  • 1
  • That works on Windows but not linux. In the end, its all about how the client terminal program honors the control message. On my linux machine, the screen will clear but my terminal emulator will not clear its history. – tdelaney Oct 14 '13 at 16:39
0

for windows:

import os
def clear(): os.system('cls')

for linux:

import os
def clear(): os.system('clear')

then to clear the terminal simply call:

clear()
Alea Kootz
  • 913
  • 4
  • 11
0

No need of importing any libraries. Simply call the cls function.

cls()
PiotrWolkowski
  • 8,408
  • 6
  • 48
  • 68
0

I am using Linux and I found it much easier to just use

import os

in the header of the code and

os.system('clear')

in the middle of my script. No lambda or whatever was needed to get it done. What I guess is important to pay attention to, is to have the import os in the header of the code, the very beginning where all other import commands are being written...

Babak D
  • 41
  • 1
  • 8
0

Adding for completeness, if you want to do this without launching an external command, do:

print('\x1bc')

Only tested on Linux.

ideasman42
  • 42,413
  • 44
  • 197
  • 320
-1

You can not control the users terminal. That's just how it works.

Think of it like write only.

Mattias
  • 451
  • 3
  • 13