如何阻止命令提示符在 python 中关闭?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14142509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop command prompt from closing in python?
提问by Aakash
I am very new to python.. I used the code
我对python很陌生..我使用了代码
x = input(" Hey what is your name " )
print(" Hey, " + x)
input(" press close to exit ")
Because i have looked for this problem on internet and came to know that you have to put some dummy input line at the end to stop the command prompt from getting closed but m still facing the problem.. pls Help
因为我已经在互联网上寻找过这个问题并且知道你必须在最后放一些虚拟输入行来阻止命令提示符关闭但我仍然面临这个问题.. 请帮助
I am using python 3.3
我正在使用 python 3.3
采纳答案by Martijn Pieters
On windows, it's the CMD console that closes, because the Python process exists at the end.
在 windows 上,关闭的是 CMD 控制台,因为 Python 进程存在于最后。
To prevent this, open the console first, then use the command line to run your script. Do this by right-clicking on the folder that contains the script, select Open console hereand typing in python scriptname.pyin the console.
为防止出现这种情况,请先打开控制台,然后使用命令行运行脚本。通过右键单击包含脚本的文件夹,选择Open console here并python scriptname.py在控制台中键入来执行此操作。
The alternative is, as you've found out, to postponethe script ending by adding a input()call at the end. This allows the user of the script to choose when the script ends and the console closes.
正如您所发现的,另一种方法是通过在末尾添加调用来推迟脚本结束input()。这允许脚本用户选择脚本结束和控制台关闭的时间。
回答by Blaze
That can be done with os module. Following is the simple code :
这可以通过 os 模块来完成。以下是简单的代码:
import os
os.system("pause")
This will generate a pause and will ask user to press any key to continue.
这将产生暂停并要求用户按任意键继续。
[edit: The above method works well for windows os. It seems to give problem with mac (as pointed by ihue, in comments). The thing is that "os" library is operating system specific and some commands might not work with one operating system like they work in another one.]
[编辑:上述方法适用于 Windows 操作系统。它似乎给 mac 带来了问题(正如 ihue 在评论中指出的那样)。问题是“os”库是特定于操作系统的,并且某些命令可能无法像在另一个操作系统中那样在一个操作系统上工作。]
回答by Inter Mob
For Windows Environments:
对于 Windows 环境:
If you don't want to go to the command prompt (or work in an environment where command prompt is restricted), I think the following solution is gooThe solution I use is to create a bat file.
如果不想进入命令提示符(或者在命令提示符受限的环境下工作),我认为下面的解决方案是goo,我使用的解决方案是创建一个bat文件。
Use notepad to create a text file. In the file the contents will look something like:
使用记事本创建文本文件。在文件中,内容将类似于:
my_python_program.py
pause
my_python_program.py
pause
Then save the file as "my_python_program.bat" - DO NOT FORGET TO SELECT "All Files!
然后将文件另存为“my_python_program.bat”——不要忘记选择“所有文件!
When you run the bat file it will run the python program and pause at the end to allow you to read the output. Then if you press any key it will close the window.
当您运行 bat 文件时,它将运行 python 程序并在最后暂停以允许您读取输出。然后,如果您按任意键,它将关闭窗口。

