logo

Pythonで画面をクリアするにはどうすればよいですか?

ほとんどの場合、(コンソールではなく) Python インタラクティブ シェル/ターミナルを操作しているときに、出力が乱雑になり、何らかの理由で画面をクリアしたくなります。インタラクティブなシェル/ターミナルでは、単純に使用できます。

ctrl+l>

しかし、Python スクリプトの実行中に画面をクリアしたい場合はどうすればよいでしょうか?残念ながら、画面をクリアするための組み込みのキーワードや関数/メソッドはありません。したがって、私たちは自分たちでそれを行います。



Windowsオペレーティングシステムでの画面のクリア

方法 1: cls を使用して Python で画面をクリアする

cls するだけで Windows の画面をクリアできます。

Python3








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

オラクルテーブルの作成
>

>

例 2: clear を使用して Python で画面をクリアする

代わりに OS のみをインポートすることもできます OSインポートシステムから ただし、その場合は system('clear') を次のように変更する必要があります OSシステム ('クリア')。

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

Javaのデフォルトパラメータ

>

>

例 3: call を使用して Python で画面をクリアする

これを実現するもう 1 つの方法は、 サブプロセスモジュール

Python3

マップC++で見つける




# 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スイングチュートリアル

Linux オペレーティング システムでの画面のクリア

この例では、 タイムモジュール そして OSモジュール Linux OS で画面をクリアします。

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

>

>