logo

파이썬에서 화면을 지우는 방법은 무엇입니까?

대부분의 경우 Python 대화형 셸/터미널(콘솔이 아님)을 사용하여 작업하는 동안 지저분한 출력으로 끝나고 어떤 이유로 화면을 지우고 싶습니다. 대화형 셸/터미널에서는 간단히 다음을 사용할 수 있습니다.

ctrl+l>

하지만 Python 스크립트를 실행하는 동안 화면을 지우고 싶다면 어떻게 해야 할까요? 아쉽게도 화면을 지우는 내장 키워드나 함수/메서드가 없습니다. 그래서 우리는 스스로 그것을 합니다.



Windows 운영 체제에서 화면 지우기

방법 1: cls를 사용하여 Python에서 화면 지우기

간단히 cls를 사용하여 Windows에서 화면을 지울 수 있습니다.

파이썬3



문자열을 정수로 변환






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

>

>

예제 2: Clear를 사용하여 Python에서 화면 지우기

대신에 os만 가져올 수도 있습니다. OS 수입 시스템에서 하지만 그렇게 하려면 시스템('clear')을 다음으로 변경해야 합니다. OS 시스템 ('분명한').

파이썬3




t 플립플롭

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

>

>

예제 3: 호출을 사용하여 Python에서 화면 지우기

이를 수행하는 또 다른 방법은 다음을 사용하는 것입니다. 하위 프로세스 모듈 .

파이썬3




각각에 대한 타이프 스크립트
# 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()>

>

>

Linux 운영 체제에서 화면 지우기

이 예에서는 시간 모듈 그리고 운영 체제 모듈 Linux OS에서 화면을 지우려면

파이썬3


문자열.하위 문자열 자바



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

>

>