Python, Press Any Key To Exit

Viewed 84298

So, as the title says, I want a proper code to close my python script. So far, I've used input('Press Any Key To Exit'), but what that does, is generate a error. I would like a code that just closes your script without using a error.

Does anyone have a idea? Google gives me the input option, but I don't want that It closes using this error:

Traceback (most recent call last):
  File "C:/Python27/test", line 1, in <module>
    input('Press Any Key To Exit')
  File "<string>", line 0

   ^
SyntaxError: unexpected EOF while parsing
10 Answers
a = input('Press a key to exit')
if a:
    exit(0)

in Windows:

if msvcrt.kbhit():
    if msvcrt.getch() == b'q':
        exit()

In python 3:

  while True: 
        #runtime..
        answer = input("ENTER something to quit: ") 
        if answer: 
            break 

You can use this code:

from os import system as console

console("@echo off&cls")

#Your main code code

print("Press Any Key To Exit")
console("pause>NUL&exit")
Related