Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep cmd.exe open

I have a python script which I have made dropable using a registry-key, but it does not seem to work. The cmd.exe window just flashes by, can I somehow make the window stay up, or save the output?

EDIT: the problem was that it gave the whole path not only the filename.

like image 1000
Viktor Mellgren Avatar asked Jul 24 '12 09:07

Viktor Mellgren


5 Answers

If you want a pure-Python solution to this, you'll have to wrap your script with a try/except clause:

import traceback
try:
    ... your script ...
except:
    traceback.print_exc()

raw_input("Press Enter to close") # Python 2
input("Press Enter to close") # Python 3

This way the window will stay open, even when your code throws an exception. It will still close when Python cannot parse the script, i.e. when you have a syntax error. If you want to fix that too, I'd suggest a good IDE that allows you to develop in a more pleasant way (PyCharm and PyDev come to mind).

like image 194
dr. Sybren Avatar answered Nov 20 '22 08:11

dr. Sybren


Insert a line at the end of your script:

raw_input("Press Enter to close") # Python 2

or

input("Press Enter to close") # Python 3
like image 25
Tim Pietzcker Avatar answered Nov 20 '22 10:11

Tim Pietzcker


When your script finished its execution, the window gets closed.

One trick to keep it open is to have it to ask the user for some kind of input, e.g. via raw_input.


So you can just add

raw_input()

to the end of your script to wait for the user to press Enter.

like image 44
sloth Avatar answered Nov 20 '22 08:11

sloth


Another possible option is to create a basic TKinter GUI with a textarea and a close button. Then run that with subprocess or equiv. and have that take the stdout from your python script executed with pythonw.exe so that no CMD prompt appears to start with.

This keeps it purely using Python stdlib's and also means you could use the GUI for setting options or entering parameters...

Just an idea.

like image 45
Jon Clements Avatar answered Nov 20 '22 09:11

Jon Clements


Either right-click your script and remove Program->Close on exit checkbox in its properties, or use cmd /k as part of its calling line.

Think twice before introducing artificial delays or need to press key - this will make your script mostly unusable in any unattended/pipe calls.

like image 1
Oleg V. Volkov Avatar answered Nov 20 '22 10:11

Oleg V. Volkov