简体   繁体   English

手动关闭程序时可以让python做些什么吗?

[英]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 您可以使用atexit模块: 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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