Atexit module or alternative to execute cleanup code at termination of program

It there a way to get code executed automatically upon completion of a python replit?
I thought that the module atexit was the solution, but it doesn’t seem to be working.

Repl link:

import atexit

def bye():
  print( "Bye!")


print("this is the end")t

Just put it at the end? If there are exceptions but a giant try: finally: around the whole thing.

Ultimately, I’d like to move the termination code within a module.

You could still do that, just call it at the end.

I thought it is a try,except?

They are different. Try except stops the error and then runs the code. Try finally defers the error, runs the code, then throws the error again.

Edit: by demands of @QwertyQwerty54, here is a link to someone else’s better explanation: Try, Except, else and Finally in Python - GeeksforGeeks

:sigh: come on dragonhunter

also tbh that was a terrible explanation to what try && finally do

1 Like