Clear terminal in Python


Question

Does any standard "comes with batteries" method exist to clear the terminal screen from a Python script, or do I have to go curses (the libraries, not the words)?

1
169
3/6/2018 2:18:07 PM

Accepted Answer

What about escape sequences?

print(chr(27) + "[2J")
98
1/21/2015 8:35:15 PM

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')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon