我一直在使用输入函数作为暂停脚本的一种方式:

print("something")
wait = input("Press Enter to continue.")
print("something")

有正式的方式吗?


当前回答

为了跨Python 2/3兼容性,你可以通过六个库使用输入:

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

其他回答

我在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...")

为了跨Python 2/3兼容性,你可以通过六个库使用输入:

import six
six.moves.input( 'Press the <ENTER> key to continue...' )
print ("This is how you pause")

input()

跨平台的方式;工作无处不在

import os, sys

if sys.platform == 'win32':
    os.system('pause')
else:
    input('Press any key to continue...')

这对我来说似乎很好(或Python 2.X中的raw_input())。或者,如果想要暂停特定的秒数,也可以使用time.sleep()。

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