Python Execute Code Before Terminates Aug 5, 2016 python def cleanup(): ... clean up script ... # on exit atexit.register(cleanup)