logo

Kako počistiti zaslon v pythonu?

Večino časa, ko delamo z interaktivno lupino/terminalom Python (ne s konzolo), končamo z neurejenim izpisom in želimo iz neznanega razloga počistiti zaslon. V interaktivni lupini/terminalu lahko preprosto uporabimo

ctrl+l>

Kaj pa, če želimo počistiti zaslon med izvajanjem skripta python? Na žalost ni vgrajene ključne besede ali funkcije/metode za čiščenje zaslona. Torej, to storimo sami.



Čiščenje zaslona v operacijskem sistemu Windows

1. način: počistite zaslon v Pythonu z uporabo cls

Lahko preprosto cls počistite zaslon v oknih.

Python3



poravnaj sliko s css






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

>

>

if else izjava java

Primer 2: Počisti zaslon v Pythonu z uporabo clear

Namesto tega lahko uvozite samo os iz uvoznega sistema os vendar s tem morate spremeniti sistem ('počisti') na os.sistem ('jasno').

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

>

>

Primer 3: Počistite zaslon v Pythonu z uporabo klica

Drug način za dosego tega je uporaba podprocesni modul .

Python3

datum pretvori v niz




.04 kot ulomek
# 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()>

>

>

Čiščenje zaslona v operacijskem sistemu Linux

V tem primeru smo uporabili časovni modul in os modul za brisanje zaslona v operacijskem sistemu Linux.

Python3




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

>

>

posodobitev v sql z pridružitvijo