我刚刚开始学习Python。当我在Windows上执行一个python脚本文件时,输出窗口出现但立即消失。我需要它停留在那里,这样我就可以分析我的输出。我怎么才能让它一直开着?


当前回答

让窗户一直开着的简单方法:

counter = 0

While (True):

    If (counter == 0):

        # Code goes here

    counter += 1

计数器是这样代码就不会重复自己。

其他回答

创建一个像dontClose()这样的函数或带有while循环的函数:

import time

def dontClose():
    n = 1
    while n > 0:
        n += 1
        time.sleep(n)
        

然后在代码之后运行函数。例如:

print("Hello, World!")
dontClose()

最简单的方法:

your_code()

while True:
   pass

当您打开窗口时,它不会关闭,直到您关闭提示符。

`import sys,traceback
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`

只需在实际代码之后编写上面的代码。如。我从用户输入和打印在控制台,因此我的代码将看起来像这样->

`import sys,traceback
nam=input("enter your name:")
print("your name is:-{}".format(nam)) #here all my actual working is done
sys.exc_info()[0]
traceback.format_exc()
print("Press Enter to exit ...")
input()`

如果你想保持cmd-window打开并且在运行文件目录下,这在Windows 10下是有效的:

cmd /k cd /d $(CURRENT_DIRECTORY) && python $(FULL_CURRENT_PATH)

Python 3

input('Press Enter to Exit...')

会成功的。