1

I'm doing some very basic lessons on Python in an online class. I was given a print task to complete, but the issue is the cmd closes immediately after the script runs, so fast that I cannot read the output.

I've done some searching and this is apparently a problem newbies have been having for a while but the solution is always to open the .py file manually using cmd - that is EXACTLY what I am doing and far from being a solution, is the exact cause of the issue. It does not do this if I click-open it with Python, and it does not close if I open it in IDLE.

As instructed, I am navigating to, and opening manually from:

cmd C:\Users\Me\Desktop\Python\myfile.py

Python is set in the environment variables/been added to the PATH. To prevent confusion, it does open in cmd, waits for my input, accepts my input, and then closes. I'm clarifying because after reading many threads, the Python community always seems to be either utterly baffled by, or give degree-level explanations to, what seem to be very simple questions.

Also for anyone wondering, I'm just running:

statement = input("Enter a statement to print: ")

if statement == "Hello world":
    exit()

else:
    print(statement)

As the task required we print anything other than 'Hello world'. I just want the output to stay open in cmd so I can read it. The brief also stipulates I cannot run the code in IDLE, so no cheating it that way. Must be Windows cmd.

4
  • put a input("Hit enter to close.") before the exit() (which should be exit(0)) and after the print(statement) to make it wait ... Commented Oct 22, 2018 at 19:05
  • Works fine for me. Are you opening a command prompt window first and then running the script from inside the command prompt? Also you should be running it with python myfile.py, not just typing in the filename (which opens the file, not runs it). Commented Oct 22, 2018 at 19:09
  • Hi Patrick, thank-you for the quick response. It now waits for my input before closing, I'm fairly happy with that. Commented Oct 22, 2018 at 19:09
  • 1
    @Engineero, Yes I was doing exactly that, woops. Didn't realise I had to include python before my filename. Combining your suggestion and Patrick's it is doing what I want it to do now. At least I learned something! Commented Oct 22, 2018 at 19:13

3 Answers 3

1

The problem is that you ask cmd to run your python script as the only task it needs to perform. When python script finishes, the cmd window is closed as the task cmd was performing has exited.

To run the script and keep output on the screen, as @Engineero have rightfully pointed out, you need to:

  1. Run the cmd. Expected result - shell opens, where you can type commands. It shall say something like C:\SOME\path>_, where _ is your cursor.

  2. Navigate to your python script by typing cd C:\Users\Me\Desktop\Python.

  3. Use python interpreter to launch your script by typing python myfile.py

When you script finishes, you shall see the output on the screen and be able to type next command. I hope that helps.

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

Comments

0

Add input("hit any key to close") at the end of your program.

that way your script will wait for an input from the user before closing.

1 Comment

thats only half the truth - and about what I commented 2 minutes ago ...
0

Instead of running as

cmd C:\Users\Me\Desktop\Python\myfile.py

Launch as

python -i C:\Users\Me\Desktop\Python\myfile.py

This way the interpreter will remain open after running the script.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.