2

I am studying about exceptions and exit codes for errors but my terminal does not give me an exit code in Python 3.8 on Windows 10.

I would like to be able to se the message "Process finished with exit code 0" from a simple "hello world" program for example.

  • https://docs.python.org/3/library/sys.html?#sys.exit - which part are you having trouble with? Welcome to SO. This isn't a discussion forum or tutorial. Please take the [tour] and take the time to read [ask] and the other links found on that page. – wwii Aug 27 '20 at 18:01
  • Related: [Exit codes in Python](https://stackoverflow.com/questions/285289/exit-codes-in-python), [How to terminate a Python script](https://stackoverflow.com/questions/73663/how-to-terminate-a-python-script), [How to throw error and exit with a custom message in python](https://stackoverflow.com/questions/22633544/how-to-throw-error-and-exit-with-a-custom-message-in-python). Many more searching with `python exit code message site:stackoverflow.com` - do any of them fit your needs? If so choose one and vote to close your question as a duplicate. – wwii Aug 27 '20 at 18:08
  • 2
    "Process finished with exit code whatever" is a message printed by PyCharm. Displaying exit codes is not your Python program's responsibility - after all, to *have* an exit code, your program must have already exited. (You can't print *before* exiting, because something might catch `SystemExit` or do something else that changes how the exit happens.) – user2357112 Aug 27 '20 at 18:34
  • 2
    You need to check the exit code from outside the program. How to do that depends on how you're running it - for example, in a Windows command prompt, you might use [`%errorlevel%`](https://stackoverflow.com/questions/334879/how-do-i-get-the-application-exit-code-from-a-windows-command-line) – user2357112 Aug 27 '20 at 18:34
  • I think Monica might have found what I needed, thanks! – Fredh Rodrigues Sep 01 '20 at 17:21

2 Answers2

2

Enjoy this Full Explanation demo.

import subprocess


result = subprocess.run("C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Write-Output 'Hello world!' ", shell=True, capture_output=True) # command to run in powershell using python subprocess module

res = result.returncode # return system-exit code of the command

out = result.stdout # return output of the powershell command


print(f"The output of the command is {out}, The exit code is {res} and the process generated by command is {result}.")

output

The output of the command is b'Hello world!\r\n', The exit code is 0 and the process generated by command is CompletedProcess(args="C:/Windows/System32/WindowsPowerShell/v1.0/powershell.exe Write-Output 'Hello world!' ", returncode=0, stdout=b'Hello world!\r\n', stderr=b'').

    
0

EDITED ANSWER : There must be some unhandled exceptions/errors in your exercise, so that it didn't exit with exit code 0 (which means successful execution of your exercise). Add Try at the start of your program and catching the exception at the end as below sample program.

print() function always returns None, so don't try getting return status from print()

import sys

try:
    print('hello world')                      #print('string') - Always returns None
    print("Process finished with exit code 0")
    sys.exit(0)
except Exception as ex:
    print("Error:"+ex)
    sys.exit(1)
Blossom
  • 113
  • 1
  • 8
  • This is not exactly what I am looking for, my question maybe more related to how different terminals handle the python programs (pycharm, cmd, powershell). I am folowing a class and the exercise has no "print("Process finished with exit code 0")" line, it just appears on the terminal at the end. – Fredh Rodrigues Sep 01 '20 at 17:14
  • when The Process finished with **exit code 0** it means that the process is successful, else there is an issue such as **exit code 1 or more**. I suggest to learn python **subprocess** module or in general **Interacting with OS using Python** –  Sep 01 '20 at 20:32
  • @FredhRodrigues The line "Processs finished with exit code 0" appears at the END because, there was no runtime exception caught while running the exercise. (**exit code 0** indicates successful running of your exercise ) whereas exit code **other than 0** may denote some specific errors/exceptions caught in the exercise – Blossom Sep 02 '20 at 08:07