La maggior parte delle volte, mentre lavoriamo con la shell/terminale interattivo Python (non una console), ci ritroviamo con un output disordinato e per qualche motivo vogliamo pulire lo schermo. In una shell/terminale interattivo, possiamo semplicemente usare
stringa confronta con Java
ctrl+l>
Ma cosa succede se vogliamo pulire lo schermo mentre eseguiamo uno script Python? Sfortunatamente, non esiste una parola chiave o una funzione/metodo incorporati per pulire lo schermo. Quindi, lo facciamo da soli.
Cancellazione dello schermo nel sistema operativo Windows
Metodo 1: cancella lo schermo in Python usando cls
Puoi semplicemente cliccare per cancellare lo schermo in Windows.
Python3
import> os> # Clearing the Screen> os.system(> 'cls'> )> |
>
>
Esempio 2: Cancella lo schermo in Python usando clear
Puoi anche importare solo il sistema operativo invece di dal sistema di importazione del sistema operativo ma con questo, devi cambiare system('clear') in os.system ('chiaro').
Python3
# import only system from os> from> os> import> system, name> # import sleep to show output for some time period> from> time> import> sleep> # define our clear function> def> clear():> > # for windows> > if> name> => => 'nt'> :> > _> => system(> 'cls'> )> > # for mac and linux(here, os.name is 'posix')> > else> :> > _> => system(> 'clear'> )> # print out some text> print> (> 'hello geeks
'> *> 10> )> # sleep for 2 seconds after printing output> sleep(> 2> )> # now call function we defined above> clear()> |
>
un array in Java
>
Esempio 3: cancella lo schermo in Python usando call
Un altro modo per ottenere questo risultato è utilizzare il file modulo del sottoprocesso .
Python3
# import call method from subprocess module> from> subprocess> import> call> # import sleep to show output for some time period> from> time> import> sleep> # define clear function> def> clear():> > # check and make call for specific operating system> > _> => call(> 'clear'> if> os.name> => => 'posix'> else> 'cls'> )> print> (> 'hello geeks
'> *> 10> )> # sleep for 2 seconds after printing output> sleep(> 2> )> # now call function we defined above> clear()> |
Java ottiene l'ora corrente
>
>
Cancellazione dello schermo nel sistema operativo Linux
In questo esempio, abbiamo utilizzato il file modulo temporale E modulo del sistema operativo per cancellare lo schermo nel sistema operativo Linux.
Python3
10 ml è quanto
import> os> from> time> import> sleep> # some text> print> (> 'a'> )> print> (> 'b'> )> print> (> 'c'> )> print> (> 'd'> )> print> (> 'e'> )> print> (> 'Screen will now be cleared in 5 Seconds'> )> # Waiting for 5 seconds to clear the screen> sleep(> 5> )> # Clearing the Screen> os.system(> 'clear'> )> |
>
>