如何在获得输出后立即阻止 python.exe 关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3591807/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:52:13  来源:igfitidea点击:

How can I stop python.exe from closing immediately after I get an output?

python

提问by Clinton

Is there any way I can stop python.exe from closing immediately after it completes? It closes faster than I can read the output.

有什么办法可以阻止 python.exe 在完成后立即关闭?它关闭的速度比我读取输出的速度要快。

Here is the program:

这是程序:

width = float(input("Enter the width: "))
height = float(input("Enter the height: "))
area = width * height
print("The area is", area, "square units.")

采纳答案by Manoj Govindan

You can't - globally, i.e. for every python program. And this is a good thing - Python is great for scripting (automating stuff), and scripts should be able to run without any user interaction at all.

你不能 - 全局,即每个 python 程序。这是一件好事——Python 非常适合编写脚本(自动化东西),脚本应该能够在没有任何用户交互的情况下运行。

However, you can always ask for input at the end of your program, effectively keeping the program alive until you press return. Use input("prompt: ")in Python 3(or raw_input("promt: ")in Python 2). Or get used to running your programs from the command line (i.e. python mine.py), the program will exit but its output remains visible.

但是,您始终可以在程序结束时要求输入,从而有效地使程序保持活动状态,直到您按回车键。使用input("prompt: ")在Python 3(或raw_input("promt: ")在Python 2)。或者习惯于从命令行运行程序(即python mine.py),程序将退出但其输出仍然可见。

回答by Manoj Govindan

It looks like you are running something in Windows by double clicking on it. This will execute the program in a new window and close the window when it terminates. No wonder you cannot read the output.

看起来您正在通过双击它在 Windows 中运行某些东西。这将在新窗口中执行程序并在它终止时关闭窗口。难怪您无法读取输出。

A better way to do this would be to switch to the command prompt. Navigate (cd) to the directory where the program is located and then call it using python. Something like this:

更好的方法是切换到命令提示符。导航 ( cd) 到程序所在的目录,然后使用python. 像这样的东西:

C:\> cd C:\my_programs\
C:\my_programs\> python area.py

Replace my_programswith the actual location of your program and area.pywith the name of your python file.

替换my_programs为程序的实际位置和area.pypython 文件的名称。

回答by msw

Auxiliary answer

辅助回答

Manoj Govindan's answer is correct but I saw that comment:

Manoj Govindan 的回答是正确的,但我看到了该评论:

Run it from the terminal.

从终端运行它。

And got to thinking about why this is so not obvious to windows users and realized it's because CMD.EXE is such a poor excuse for a shell that it should start with:

并开始思考为什么这对 Windows 用户来说如此不明显,并意识到这是因为 CMD.EXE 是一个糟糕的 shell 借口,它应该以:

Windows command interpreter copyright 1999 Microsoft
Mein Gott!! Whatever you do, don't use this!!
C:>

Windows 命令解释器版权所有 1999 Microsoft
Mein Gott!不管你做什么,不要用这个!!
C:>

Which leads me to point at https://stackoverflow.com/questions/913912/bash-shell-for-windows

这让我指向https://stackoverflow.com/questions/913912/bash-shell-for-windows

回答by kranthi kumar

Just declare a variable like kor mor any other you want, now just add this piece of code at the end of your program

只需声明一个像km或任何其他你想要的变量,现在只需在程序末尾添加这段代码

k=input("press close to exit") 

Here I just assumed kas variable to pause the program, you can use any variable you like.

这里我只是假设k作为暂停程序的变量,你可以使用任何你喜欢的变量。

回答by usr72927328

In windows, if Python is installed into the default directory (For me it is):

在 Windows 中,如果 Python 安装到默认目录中(对我来说是):

cd C:\Python27

You then proceed to type

然后你继续输入

"python.exe "[FULLPATH]\[name].py" 

to run your Python script in Command Prompt

在命令提示符中运行你的 Python 脚本

回答by Jester

Python files are executables, which means that you can run them directly from command prompt(assuming you have windows). You should be able to just enter in the directory, and then run the program. Also, (assuming you have python 3), you can write:

Python 文件是可执行文件,这意味着您可以直接从命令提示符运行它们(假设您有 Windows)。您应该可以直接进入目录,然后运行该程序。另外,(假设你有 python 3),你可以写:

input("Press enter to close program")

and you can just press enter when you've read your results.

阅读结果后,您只需按 Enter 键即可。

回答by sarasimple

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 better than inserting code into python that asks you to press any key - because if the program crashes before it reaches that point, the window closes and you lose the crash info. The solution I use is to create a bat file.

如果您不想进入命令提示符(或在命令提示符受限的环境中工作),我认为以下解决方案比将代码插入要求您按任意键的 python 中更好 - 因为如果程序崩溃在到达该点之前,窗口将关闭,您将丢失崩溃信息。我使用的解决方案是创建一个bat文件。

Use notepad to create a text file. In the file the contents will look something like:

使用记事本创建文本文件。在文件中,内容将类似于:

my_python_program.py
pause

Then save the file as "my_python_program.bat"

然后将文件另存为“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 程序并在最后暂停以允许您读取输出。然后,如果您按任意键,它将关闭窗口。