Clear terminal in Python

Stefano Borini picture Stefano Borini · Jan 18, 2010 · Viewed 380.6k times · Source

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

Answer

poke picture poke · Jan 18, 2010

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