engise226
engise226

Reputation: 29

Can I let python do something when manually close the program?

For instance, let the program write down a log file before I manually stop it.

Upvotes: 1

Views: 258

Answers (1)

avysk
avysk

Reputation: 2045

You can use atexit module: https://docs.python.org/3.5/library/atexit.html

Example:

import atexit
import time

def exit_hook():
    print("Exiting!")

atexit.register(exit_hook)

while True:
    print("looping...")
    time.sleep(1)

Upvotes: 4

Related Questions