简体   繁体   中英

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

例如,让该程序记下日志文件,然后再手动停止它。

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM