import atexit
import sys
@atexit.register
def goodbye():
print "Good Bye"
print "Hello World"
#sys.exit(0) or sys.exit(1)
In the code above, Good Bye is executed every time it exits. Can I print a different message depending on the exit status? Something like if the exit status is 1, print FATAL: Good Bye
I checked sys.exitfunc too but that also doesn’t seem to support this? Isn’t this something basic that should be supported or is there something better to use for cases like these. Is using a wrapper function to exit my program the only way?
You can monkey-patch
sys.exitpretty easily:Of course, this only runs if
sys.exitis actually called from somewhere within the code …You could alternatively wrap your main function in a try/except: