11

I am very new to python.. I used the code

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

4
  • you can either run the script through pythons idle platform, so it wont 'close' or you can open a command line (cmd) and then run your script from there, it wont 'close' Commented Jan 3, 2013 at 16:03
  • 1
    I'm not sure what you're looking for. does python -i script.py do what you want? What command prompt is closing? The python command prompt or the shell that you're running python through? Commented Jan 3, 2013 at 16:03
  • hum, i think i know the problem: are you giving an input after the second "input" statement? the purpose of the input command is to hang ("stay there and not close") untill the user presses a key. if you give an input, it's closing. Commented Jan 3, 2013 at 16:05
  • No i am giving the first input then second line i am asking for python to print it.. then i m asking again python for an input so that it stays there.. It works perfect on idle but not on cmd Commented Jan 3, 2013 at 16:09

5 Answers 5

24

On windows, it's the CMD console that closes, because the Python process exists at the end.

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.

The alternative is, as you've found out, to postpone the 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.

Sign up to request clarification or add additional context in comments.

16 Comments

Note that you (at least on Windows 7) need to shift + right-click on the directory containing your source files, not the script itself.
Ya i tried that too but still the command prompt closed instantly even after adding an input
@user1946082: Sounds like you created an error in your script instead. An input() call, by itself, does not just exit Python without input.
Well, it doesn't wait on my system, like the OP's, either. There is no exception as far as I can see.
@martineau: Use python 3 instead of 2, or use raw_input().
|
16

That can be done with os module. 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. 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.]

3 Comments

"os" library is operating system specific. I am using this with windows and it works just fine because there's actually a "pause" command in cmd. Maybe, linux has some other way of achieving this.
All right, Thanks for your clarification. I'll check out how to do this specifically on a Mac, but you should have mentioned that as part of your answer.
True ihue, I added that. Thanks for raising this. Please share your findings, as well, for mac.
3

For Windows Environments:

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.

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!

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.

Comments

2

Just Add Simple input At The End Of Your Program it Worked For Me

input()

Try it And It Will Work Correctly

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.