Restarting scripts

Peter Hansen peter at engcorp.com
Mon Jan 16 20:39:48 EST 2006


Brian Cole wrote:
> If everything dies by a Python exception...
> 
> if __name__ == '__main__':
>     try:
>         main(sys.argv)
>     except:
>         os.execlp("python", "python", sys.argv)
> 
> Pretty nasty peice of code, but it works well for restarting your script.

Noting that sys.exit() and possibly several other things can "raise 
SystemExit" and while it is a subclass of Exception (and gets caught by 
the above), it is usually considered a clean and intentional exit and 
changing the above to do this is probably best:

try:
    main(sys.argv)
except SystemExit:
    raise
except:
    os.execlp(.... etc etc

-Peter




More information about the Python-list mailing list