Question: Read text to generate a list. list.pop() while the program is running. Now I want to ask if I can rewrite the values in the list into the text when exiting abnormally. Exception: Ctrl-c, kill, Ctrl-z Thank you!
The abnormal situations you mentioned are all caused by the program receiving the corresponding signal and taking the default action (exit), so you can use 注册信号, 改变默认动作 to avoid the program from exiting abnormally.
After the above signal将对应的信号动作绑定到hander函数, 在接受到Ctrl-c , kill , Ctrl-z都能分别执行handler的代码了, 至于想怎么实现, 可以自定义, 如果除了这些信号意外的情况, 得示其他情况而决定采取什么措施! 可以学习下python signalrelated knowledge (some signals cannot be captured, this is determined by the system, you need to pay attention)
The abnormal situations you mentioned are all caused by the program receiving the corresponding signal and taking the default action (exit), so you can use
注册信号, 改变默认动作
to avoid the program from exiting abnormally.After the above
signal
将对应的信号动作绑定到hander函数
, 在接受到Ctrl-c , kill , Ctrl-z都能分别执行handler
的代码了, 至于想怎么实现, 可以自定义, 如果除了这些信号意外的情况, 得示其他情况而决定采取什么措施! 可以学习下python signal
related knowledge (some signals cannot be captured, this is determined by the system, you need to pay attention)You can try to catch the exception in if main:
There is a function in Python called atexit, which will call back when the program exits. General exits can be captured.