Tharix Tharix - 7 months ago 85
Python Question

Python: Clear screen in shell

Just a quick question:

How do you clear the screen in shell?
I've seen ways like:

import os
os.system('cls')


This just opens the windows cmd, clears the screen and closes but
I want the shell window to be cleared

(PS: I don't know this helps, but I'm using version 3.3.2 of Python)

Thank you :)

Answer

For OS X, you can use subprocess module and call 'cls' from shell:

import subprocess as sp
sp.call('cls',shell=True)

To prevent '0' from showing on top of the window, replace the 2nd line with:

tmp = sp.call('cls',shell=True)

For linux, you must replace cls command with clear

tmp = sp.call('clear',shell=True)