【发布时间】:2012-07-18 03:14:00
【问题描述】:
我一直在使用 input 函数来暂停我的脚本:
print("something")
wait = input("Press Enter to continue.")
print("something")
有正式的方法吗?
【问题讨论】:
-
在调试器中工作并使用断点?
我一直在使用 input 函数来暂停我的脚本:
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")
【讨论】:
print 显示长文本块,然后根据您的Python 版本使用input() 或raw_input('Press <ENTER> to continue')。
input()(或Python 2.x上的raw_input())来提示用户,而不是延时。快速的读者不想等待延迟,慢的读者可能想要更多的延迟时间,有人可能在阅读时被打断并想要更多的时间,等等。另外,如果有人经常使用该程序,他/她可能会习惯它的工作方式,甚至不需要阅读长文本。让用户控制文本块显示多长时间以供阅读更友好。
【讨论】:
仅适用于 Windows,请使用:
import os
os.system("pause")
【讨论】:
pause,而不是在 python 文件末尾添加命令,您将得到同样的效果。这样,python 文件是独立于操作系统的,而批处理文件只能在 Windows 中使用。
我有一个类似的问题,我正在使用信号:
import signal
def signal_handler(signal_number, frame):
print "Proceed ..."
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
因此,您为信号 SIGINT 注册了一个处理程序并暂停等待任何信号。现在从您的程序外部(例如在 bash 中),您可以运行 kill -2 <python_pid>,它会将信号 2(即 SIGINT)发送到您的 python 程序。您的程序将调用您注册的处理程序并继续运行。
【讨论】:
signal.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
【讨论】:
我认为停止执行的最好方法是 time.sleep() 函数。
如果您只需要在某些情况下暂停执行,您可以简单地实现一个 if 语句,如下所示:
if somethinghappen:
time.sleep(seconds)
您可以将 else 分支留空。
【讨论】:
很简单:
raw_input("Press Enter to continue ...")
print("Doing something...")
【讨论】:
NameError: name 'raw_input' is not defined
Print ("This is how you pause")
input()
【讨论】:
NameError: name 'Print' is not defined
print。您甚至可以在函数名和左括号之间保留空格而不会出错。
我对 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...")
【讨论】:
在 Linux 中,您可以向后台发出 kill -TSTP <pid> 并停止进程。所以,它就在那里,但不消耗 CPU 时间。
然后,kill -CONT <pid> 关闭并再次运行。
【讨论】:
为了跨 Python 2/3 兼容性,您可以通过 six 库使用 input:
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
注意:你可以安装keyboard模块,只需在你的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())
这会产生一个解释器,其行为几乎与真正的解释器完全相同,包括当前上下文,只有输出:
暂停。按 ^D (Ctrl+D) 继续。 >>>
Python Debugger 也是暂停的好方法。
import pdb
pdb.set_trace() # Python 2
或
breakpoint() # Python 3
【讨论】: