Keep cmd.exe open

I have a python script that I made dropable with a registry key, but it does not seem to work. The cmd.exe window just blinks, can I somehow make the window stop or save the output?

EDIT: The problem was that it gave the whole path, not just the file name.

+6
source share
5 answers

If you need a pure-Python solution, you will 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 

Thus, the window will remain open, even if your code throws an exception. It will still be closed when Python cannot parse the script, that is, when you have a syntax error. If you want to fix this, I would suggest a good IDE that will allow you to develop in a more pleasant way ( PyCharm and PyDev come to mind).

+6
source

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 
+2
source

When your script has completed execution, the window closes.

One trick to keep it open is to ask the user to enter some input, for example. 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
source

Another possible option is to create a basic TKinter GUI with a text area and a close button. Then run this using a subprocess or equiv. and take this stdout from your python script executed with pythonw.exe so that no CMD hint starts with.

This saves it purely with Python stdlib, and also means that you can use the GUI to configure parameters or enter parameters ...

Just an idea.

+1
source

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

Think twice before entering artificial delays or you need to press a key - this will make your script basically unsuitable for any calls without participation / pipes.

+1
source

Source: https://habr.com/ru/post/921185/


All Articles