sorin sorin - 7 months ago 60
Python Question

How to exit from Python without traceback?

I would like to know how to I exit from Python without having an traceback dump on the output.

I still want want to be able to return an error code but I do not want to display the traceback log.

I want to be able to exit using

without trace but in case of an Exception (not an exit) I want the trace.

jkp jkp

You are presumably encountering an exception and the program is exiting because of this (with a traceback). The first thing to do therefore is to catch that exception, before exiting cleanly (maybe with a message, example given).

Try something like this in your main routine:

import sys, traceback

def main():
        do main program stuff here
    except KeyboardInterrupt:
        print "Shutdown requested...exiting"
    except Exception:

if __name__ == "__main__":