Clear terminal in Python
Why hasn't anyone talked about just simply doing Ctrl+L in Windows or Cmd+L in Mac. Surely the simplest way of clearing screen.
What about escape sequences?
print(chr(27) + "[2J")
A simple and cross-platform solution would be to use either the cls
command on Windows, or clear
on Unix systems. Used with os.system
, this makes a nice one-liner:
import os
os.system('cls' if os.name == 'nt' else 'clear')
As for me, the most elegant variant:
import os
os.system('cls||clear')