How to run one last function before getting killed in Python?

前端 未结 5 1940
不思量自难忘°
不思量自难忘° 2020-12-08 20:17

Is there any way to run one last command before a running Python script is stopped by being killed by some other script, keyboard interrupt etc.

5条回答
  •  执念已碎
    2020-12-08 20:45

    Use the atexit module to register a function that will be called at the end.

    import atexit
    atexit.register(some_function)
    

提交回复
热议问题