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)?
In an interactive shell/terminal, we can simply use ctrl+l to clear the screen.
You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators.
In Python sometimes we have link the output and we want to clear the screen in the cell prompt we can clear the screen by pressing Control + l .
From os import system. Define a function. Make a system call with 'clear' in Linux and 'cls' in Windows as an argument. Store the returned value in an underscore or whatever variable you want (an underscore is used because python shell always stores its last output in an underscore).
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With