0

I tried running a python script, but it immediately closes when it encounters an error. Is there a way to stop the console window from closing after an error without using a batch file and without typing this command:

C:\WINDOWS\system32\cmd.exe /K <command>

By the way, adding try and except still doesn't stop the console window from closing. Even using:

except:
  sys.exit(0)

1 Answer 1

1

Well the console closes as soon as the python program is finished. So if you sys.exit(0) in your except statement, the program finishes and the console close.

Either you write a .bat file next to your python file, that makes sure to call cmd.exe properly, and you run that one. Or you wait in the except statement for some user input/confirmation and sys.exit() only then.

Last solution if you want to see the console only to see the messages, try to print the messages to a file, either using print( "abc", file=opened_file ) in the python code, or adding > filename at the end of the shell command to redirect the standard output to a file.

Personally, I just run the program directly from cmd.exe's shell and never close the terminal.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.