IT박스

파이썬 프로그램을 일시 정지하는 올바른 방법

itboxs 2020. 6. 18. 21:33
반응형

파이썬 프로그램을 일시 정지하는 올바른 방법


스크립트를 일시 중지하는 방법으로 입력 기능을 사용하고 있습니다.

print("something")
wait = input("PRESS ENTER TO CONTINUE.")
print("something")

이것을 수행하는 공식적인 방법이 있습니까?


나에게 잘 보인다 (또는 raw_input()Python 2.X에서). 또는 time.sleep()특정 시간 (초) 동안 일시 중지하려는 경우 사용할 수 있습니다 .

import time
print("something")
time.sleep(5.5)    # pause 5.5 seconds
print("something")

창에만 사용하십시오 :

import os
os.system("pause")

입력하지 않고 일시 중지하고 싶다고 가정합니다.

사용하다

시간 수면 (초)


그래서, 나는 이것이 코딩 작업에서 잘 작동한다는 것을 알았습니다. 프로그램 시작시 간단히 함수만들었습니다.

def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

이제 pause()배치 파일을 작성하는 것처럼 필요할 때마다 함수 를 사용할 수 있습니다 . 예를 들어 , 다음 과 같은 프로그램에서 :

import os
import system

def pause():
    programPause = raw_input("Press the <ENTER> key to continue...")

print("Think about what you ate for dinner last night...")
pause()

분명히이 프로그램은 목표가 없으며 단지 예시 목적 일뿐입니다. 그러나 당신은 내가 의미하는 바를 정확하게 이해할 수 있습니다.

참고 : Python 3의 경우 input와 반대로 사용해야 합니다.raw_input


나는 비슷한 질문을했고 신호를 사용하고 있었다 :

import signal

def signal_handler(signal_number, frame):
    print "Proceed ..."

signal.signal(signal.SIGINT, signal_handler)
signal.pause()

따라서 SIGINT 신호 처리기를 등록하고 신호 대기를 일시 중지합니다. 이제 프로그램 외부 (예 : bash)에서을 실행하면 kill -2 <python_pid>python 프로그램에 신호 2 (예 : SIGINT)가 전송됩니다. 프로그램은 등록 된 핸들러를 호출하고 실행을 계속합니다.


python사용자가 누를 때까지 2와 3에 다음을 사용하여 코드 실행을 일시 중지합니다.ENTER

import six
if six.PY2:
    raw_input("Press the <ENTER> key to continue...")
else:
    input("Press the <ENTER> key to continue...")

매우 간단합니다 :

raw_input("Press Enter to continue ...")
exit()

Print ("This is how you pause")

input()

mhawkesteveha 의 의견에서 지적 했듯이이 정확한 질문에 대한 가장 좋은 대답은 다음과 같습니다.

For a long block of text, it is best to use input('Press <ENTER> to continue') (or raw_input('Press <ENTER> to continue') on Python 2.x) to prompt the user, rather than a time delay. Fast readers won't want to wait for a delay, slow readers might want more time on the delay, someone might be interrupted while reading it and want a lot more time, etc. Also, if someone uses the program a lot, he/she may become used to how it works and not need to even read the long text. It's just friendlier to let the user control how long the block of text is displayed for reading.


i think that the best way to stop the execution is the time.sleep() function. if you need to suspend the execution only in certain cases you can simply implement an if statement like this:

if somethinghappen:
    time.sleep(seconds)

you can leave the else branch empty.


I think I like this soln.

import getpass
getpass.getpass("Press Enter to Continue")

It hides whatever the user types in, which helps clarify that input is not used here.

But be mindful in OSX platform it displays a key which may be confusing.

It shows a key, like I said


Probably the best solution would be to do something similar to the getpass module yourself, without making a read -s call. Maybe making the fg color match the bg?


For cross Python 2/3 compatibility, you can use input via the six library:

import six
six.moves.input( 'Press the <ENTER> key to continue...' )

참고URL : https://stackoverflow.com/questions/11552320/correct-way-to-pause-python-program

반응형