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.
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).
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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With