在得到输出后,如何阻止python.exe立即关闭? [重复]

时间:2021-06-28 20:48:03

This question already has an answer here:

这个问题在这里已有答案:

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.")

7 个解决方案

#1


42  

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.

但是,您可以随时在程序结束时询问输入,从而有效地保持程序存活,直到您按下返回。在Python 3中使用输入(“prompt:”)(或在Python 2中使用raw_input(“promt:”))。或习惯从命令行运行程序(即python mine.py),程序将退出,但其输出仍然可见。

#2


12  

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

只需声明一个像k或m或任何其他你想要的变量,现在只需在程序结束时添加这段代码即可

k=input("press close to exit") 

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

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

#3


5  

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://*.com/questions/913912/bash-shell-for-windows

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

#4


4  

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_programs with the actual location of your program and area.py with the name of your python file.

将my_programs替换为程序的实际位置,将area.py替换为python文件的名称。

#5


4  

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程序并在结尾处暂停以允许你读取输出。然后,如果按任意键,它将关闭窗口。

#6


0  

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脚本

#7


0  

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键。

#1


42  

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.

但是,您可以随时在程序结束时询问输入,从而有效地保持程序存活,直到您按下返回。在Python 3中使用输入(“prompt:”)(或在Python 2中使用raw_input(“promt:”))。或习惯从命令行运行程序(即python mine.py),程序将退出,但其输出仍然可见。

#2


12  

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

只需声明一个像k或m或任何其他你想要的变量,现在只需在程序结束时添加这段代码即可

k=input("press close to exit") 

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

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

#3


5  

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://*.com/questions/913912/bash-shell-for-windows

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

#4


4  

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_programs with the actual location of your program and area.py with the name of your python file.

将my_programs替换为程序的实际位置,将area.py替换为python文件的名称。

#5


4  

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程序并在结尾处暂停以允许你读取输出。然后,如果按任意键,它将关闭窗口。

#6


0  

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脚本

#7


0  

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键。