Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear terminal in Python [duplicate]

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

like image 944
Stefano Borini Avatar asked Jan 18 '10 07:01

Stefano Borini


People also ask

How do you clear a Python terminal?

In an interactive shell/terminal, we can simply use ctrl+l to clear the screen.

How do you clear in terminal?

You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators.

Is there clear screen in Python?

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 .

How do you use cls in Python?

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


1 Answers

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') 
like image 71
poke Avatar answered Oct 14 '22 13:10

poke