我一直在使用输入函数作为暂停脚本的一种方式:
print("something")
wait = input("Press Enter to continue.")
print("something")
有正式的方式吗?
我一直在使用输入函数作为暂停脚本的一种方式:
print("something")
wait = input("Press Enter to continue.")
print("something")
有正式的方式吗?
这对我来说似乎很好(或Python 2.X中的raw_input())。或者,如果想要暂停特定的秒数,也可以使用time.sleep()。
import time
print("something")
time.sleep(5.5) # Pause 5.5 seconds
print("something")
正如mhawke和steveha的评论所指出的,这个问题的最佳答案是:
Python 3. x:
input('Press <ENTER> to continue')
Python 2. x:
raw_input('Press <ENTER> to continue')
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.
轶事:曾经有一段时间,程序使用“按[ANY]键继续”。这个失败了,因为人们抱怨他们在键盘上找不到ANY键:)
我有一个类似的问题,我使用信号:
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)。您的程序将调用已注册的处理程序并继续运行。
所以,我发现这在我的编程工作中非常有效。我只是在程序的最开始创建了一个函数,
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
我认为停止执行的最好方法是time.sleep()函数。
如果你只需要在某些情况下暂停执行,你可以简单地实现一个If语句,就像这样:
if somethinghappen:
time.sleep(seconds)
您可以让else分支为空。
我在Python 2和Python 3中使用以下代码暂停代码执行,直到用户按下Enter
import six
if six.PY2:
raw_input("Press the <Enter> key to continue...")
else:
input("Press the <Enter> key to continue...")
我想我喜欢这个解决方案:
import getpass
getpass.getpass("Press Enter to Continue")
它隐藏了用户输入的任何内容,这有助于澄清这里没有使用输入。
但是要注意OS X平台。它显示了一个可能令人困惑的键。
最好的解决方案可能是自己执行类似getpass模块的操作,而不执行read -s调用。也许让前景色与背景颜色相匹配?
为了跨Python 2/3兼容性,你可以通过六个库使用输入:
import six
six.moves.input( 'Press the <ENTER> key to continue...' )
通过这个方法,你可以通过按任何指定的键来恢复你的程序:
import keyboard
while True:
key = keyboard.read_key()
if key == 'space': # You can put any key you like instead of 'space'
break
同样的方法,但方式不同:
import keyboard
while True:
if keyboard.is_pressed('space'): # The same. you can put any key you like instead of 'space'
break
注意:你可以通过在shell或cmd中写这个命令来安装键盘模块:
pip install keyboard
跨平台的方式;工作无处不在
import os, sys
if sys.platform == 'win32':
os.system('pause')
else:
input('Press any key to continue...')
我和喜欢简单解决方案的非程序员一起工作:
import code
code.interact(banner='Paused. Press ^D (Ctrl+D) to continue.', local=globals())
这产生了一个几乎完全像真正的解释器一样的解释器,包括当前上下文,只有输出:
Paused. Press ^D (Ctrl+D) to continue. >>>
Python调试器也是暂停的好方法。
import pdb
pdb.set_trace() # Python 2
or
breakpoint() # Python 3
user12532854建议使用keyboard.readkey(),但它需要特定的键(我试图在没有输入参数的情况下运行它,但它最终立即返回'enter',而不是等待击键)。
通过以不同的方式表达问题(在python中寻找getchar()等效),我发现在探索由这个答案提示的readchar包后,readchar.readkey()做到了这一点。
import readchar
readchar.readkey()