logo

¿Cómo borrar la pantalla en Python?

La mayoría de las veces, mientras trabajamos con el shell/terminal interactivo de Python (no una consola), terminamos con una salida desordenada y queremos borrar la pantalla por alguna razón. En un shell/terminal interactivo, simplemente podemos usar

ctrl+l>

Pero, ¿qué pasa si queremos borrar la pantalla mientras ejecutamos un script en Python? Desafortunadamente, no hay ninguna palabra clave ni función/método integrado para borrar la pantalla. Entonces lo hacemos por nuestra cuenta.



Borrar pantalla en el sistema operativo Windows

Método 1: borrar la pantalla en Python usando cls

Simplemente puede hacer clic para borrar la pantalla en Windows.

Python3



comentario multilínea powershell






import> os> # Clearing the Screen> os.system(>'cls'>)>

>

>

comparar con cadena

Ejemplo 2: borrar pantalla en Python usando clear

También puedes importar solo os en lugar de desde el sistema de importación del sistema operativo pero con eso, tienes que cambiar el sistema('clear') a sistema operativo ('claro').

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

>

es igual a java

>

Ejemplo 3: borrar pantalla en Python usando call

Otra forma de lograr esto es utilizando el módulo de subproceso .

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

>

>

ordenar lista de matrices en java

Borrar pantalla en el sistema operativo Linux

En este ejemplo, utilizamos el módulo de tiempo y módulo del sistema operativo para borrar la pantalla en el sistema operativo Linux.

Python3




cómo convertir caracteres a cadenas
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'>)>

>

>