1

I've tried many ways to clear python interpreter screen like:

import os
os.system("cls")

but couldn't do so, please tell me how to do it.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90

2 Answers2

6
>>> import os
>>> os.system("cls")

Should work on windows.


If you are using Linux, you might use clear instead of cls.

Uriel
  • 15,579
  • 6
  • 25
  • 46
  • i just tried it again on my windows os and it gives me a `0` value , is this window specific ? – Pavneet_Singh Oct 26 '16 at 16:45
  • I'm using Windows, and i think i have some environment variables messed up, i'v installed cocos 2d a few days earlier and it require to add some environment variables. after that i'm unable to clear console both on c++(visual studio) and python. Have you anything on mind ?????? – M. Nabeel Rehman Oct 27 '16 at 14:57
2

os.system("cls") will work only on windows machine. If you are UNIX user, do:

  • Using escape sequence:

    print(chr(27) + "[2J")
    
  • Alternatively, if you are Unix user (not Windows), you may also do:

    import sys
    sys.stderr.write("\x1b[2J\x1b[H")
    # Code for clear screen in UNIX machines
    
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
  • Dude i tried many on my xp os but none worked including the one mentioned here , maybe they are os specific as agreed by uriel eli too – Pavneet_Singh Oct 27 '16 at 02:40
  • sys.stderr.write("\x1b[2J\x1b[H") for the best answer in clearing the entire screen in Linux/UNIX without any white space or 0 response. – hobbes Mar 20 '18 at 17:55