简体   繁体   English

如何阻止命令提示符在 python 中关闭?

[英]How to stop command prompt from closing in python?

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

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 here and typing in python scriptname.py in the console.为此,请右键单击包含脚本的文件夹,选择Open console here并在Open console here键入python scriptname.py

The alternative is, as you've found out, to postpone the script ending by adding a input() call at the end.正如您所发现的,另一种方法是通过在末尾添加input()调用来推迟脚本结束。 This allows the user of the script to choose when the script ends and the console closes.这允许脚本用户选择脚本结束和控制台关闭的时间。

That can be done with os module.这可以通过 os 模块来完成。 Following is the simple code :以下是简单的代码:

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. [编辑:上述方法适用于 Windows 操作系统。 It seems to give problem with mac (as pointed by ihue, in comments).它似乎给 mac 带来了问题(正如 ihue 在评论中指出的那样)。 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.]问题是“os”库是特定于操作系统的,并且某些命令可能无法像在另一个操作系统中那样在一个操作系统上工作。]

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

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.当您运行 bat 文件时,它将运行 python 程序并在最后暂停以允许您读取输出。 Then if you press any key it will close the window.然后,如果您按任意键,它将关闭窗口。

Try this,试试这个,

import sys

status='idlelib' in sys.modules

# Put this segment at the end of code
if status==False:
    input()

This will only stop console window, not the IDLE.这只会停止控制台窗口,而不是 IDLE。

Just Add Simple input At The End Of Your Program it Worked For Me只需在程序结束时添加简单的输入,它对我有用

input()

Try it And It Will Work Correctly试试看,它会正常工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM