Python, Press Any Key To Exit

Joppe De Cuyper picture Joppe De Cuyper · Aug 9, 2012 · Viewed 65.9k times · Source

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

Answer

vikki picture vikki · Aug 9, 2012

If you are on windows then the cmd pause command should work, although it reads 'press any key to continue'

import os
os.system('pause')

The linux alternative is read, a good description can be found here