6

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.

5 Answers 5

6

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).

2
  • Thanks, I ended up doing that before your post but thanks anyway :) Commented Jul 24, 2012 at 9:36
  • 1
    Maybe put the (raw_)input below the print_exc() call on the same level. Then it will pause only if there is an exception.
    – glglgl
    Commented Jul 24, 2012 at 9:42
2

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
0
1

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.

1

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.

1

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.