Quiero que mi script espere hasta que el usuario presione cualquier tecla.
¿Cómo lo hago?
En Python 3, no existe raw_input()
. Por lo tanto, sólo tiene que utilizar:
input("Press Enter to continue...")
En Python 2, debes usar raw_input()
, ya que input(prompt)
es equivalente a eval(raw_input(prompt))
:
raw_input("Press Enter to continue...")
Sin embargo, esto sólo espera a que el usuario presione "enter", por lo que es posible que desee utilizar msvcrt ((sólo para Windows/DOS) El módulo msvcrt le da acceso a una serie de funciones en la biblioteca de tiempo de ejecución de Microsoft Visual C/C++ (MSVCRT)):
import msvcrt as m
def wait():
m.getch()
Esto debería esperar a que se pulse una tecla.
Una forma de hacer esto en Python 2, es utilizar raw_input()
:
raw_input("Press Enter to continue...")
En python3 es simplemente input()
.
El [manual] de python1 proporciona lo siguiente:
import termios, fcntl, sys, os
fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", repr(c)
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
que puede ser enrollado en su caso de uso.