16

所以,正如标题所说,我想要一个正确的代码来关闭我的 python 脚本。到目前为止,我已经使用input('Press Any Key To Exit')了 ,但是这样做会产生错误。我想要一个只关闭你的脚本而不使用错误的代码。

有人有想法吗?谷歌给了我输入选项,但我不希望它使用这个错误关闭:

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
4

8 回答 8

18

如果您在 Windows 上,则 cmdpause命令应该可以工作,尽管它显示“按任意键继续”

import os
os.system('pause')

linux替代方案是read,可以在这里找到一个很好的描述

于 2012-08-09T05:13:47.973 回答
13

此语法错误是由input在 Python 2 上使用引起的,它将尝试eval在终端提示符下键入的任何内容。如果你按下enter了,那么 Python 基本上会尝试评估一个空字符串,eval(""),这会导致 aSyntaxError而不是通常的NameError.

如果您对“any”键作为回车键感到满意,那么您可以简单地将其换成raw_input

raw_input("Press Enter to continue")

请注意,在 Python 3 上raw_input已重命名为input.

对于在搜索中发现这个问题的用户,他们确实希望能够按任意退出提示并且不限于使用enter,您可以考虑使用 3rd-party 库作为跨平台解决方案。我推荐readchar可以使用pip install readchar. 它适用于 Linux、macOS 和 Windows 以及 Python 2 或 Python 3。

import readchar
print("Press Any Key To Exit")
k = readchar.readchar()
于 2012-08-09T04:03:43.570 回答
3

如果可以避免使用 python 中特定于平台的函数,我会劝阻它们,但你可以使用内置msvcrt模块。

from msvcrt import getch

junk = getch() # Assign to a variable just to suppress output. Blocks until key press.
于 2012-08-09T05:27:22.230 回答
3

A little late to the game, but I wrote a library a couple years ago to do exactly this. It exposes both a pause() function with a customizable message and the more general, cross-platform getch() function inspired by this answer.

Install with pip install py-getch, and use it like this:

from getch import pause
pause()

This prints 'Press any key to continue . . .' by default. Provide a custom message with:

pause('Press Any Key To Exit.')

For convenience, it also comes with a variant that calls sys.exit(status) in a single step:

pause_exit(0, 'Press Any Key To Exit.')

Check it out.

于 2015-11-28T01:10:14.510 回答
2
a = input('Press a key to exit')
if a:
    exit(0)
于 2019-12-17T20:52:05.880 回答
1

这是一种通过按 *nix 上的任意键来结束的方法,不显示该键并且不按 return。(通用方法的功劳归功于Python read a single character from the user。)从 SO 的角度来看,您似乎可以使用该msvcrt模块在 Windows 上复制此功能,但我没有将它安装在任何地方进行测试。过度评论以解释发生了什么......

import sys, termios, tty

stdinFileDesc = sys.stdin.fileno() #store stdin's file descriptor
oldStdinTtyAttr = termios.tcgetattr(stdinFileDesc) #save stdin's tty attributes so I can reset it later

try:
    print 'Press any key to exit...'
    tty.setraw(stdinFileDesc) #set the input mode of stdin so that it gets added to char by char rather than line by line
    sys.stdin.read(1) #read 1 byte from stdin (indicating that a key has been pressed)
finally:
    termios.tcsetattr(stdinFileDesc, termios.TCSADRAIN, oldStdinTtyAttr) #reset stdin to its normal behavior
    print 'Goodbye!'
于 2012-08-09T05:31:32.737 回答
0

Ok I am on Linux Mint 17.1 "Rebecca" and I seem to have figured it out, As you may know Linux Mint comes with Python installed, you cannot update it nor can you install another version on top of it. I've found out that the python that comes preinstalled in Linux Mint is version 2.7.6, so the following will for sure work on version 2.7.6. If you add raw_input('Press any key to exit') it will not display any error codes but it will tell you that the program exited with code 0. For example this is my first program. MyFirstProgram. Keep in mind it is my first program and I know that it sucks but it is a good example of how to use "Press any key to Exit" BTW This is also my first post on this website so sorry if I formatted it wrong.

于 2015-03-08T18:40:09.287 回答
0

in Windows:

if msvcrt.kbhit():
    if msvcrt.getch() == b'q':
        exit()
于 2018-01-29T06:59:45.943 回答