Clear terminal in Python

Clear terminal in Python

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 == nt else clear)

What about escape sequences?

print(chr(27) + [2J)

Clear terminal in Python

Why hasnt anyone talked about just simply doing Ctrl+L in Windows or Cmd+L in Mac.
Surely the simplest way of clearing screen.

Leave a Reply

Your email address will not be published. Required fields are marked *