if someone press the X button on the tab in the program or simply do ctr-c if its command line program then how do i handle the closing event for example if i want to write you have left the program by your self in a file after he close the program . is there any library or function for that?
Asked
Active
Viewed 1,245 times
1
-
For ctrl+C you can `try`-`except KeyboardInterrupt`. What do you mean by "press the X button on the tab in the program"? – nog642 Jul 12 '20 at 21:06
1 Answers
1
You can register a function to be executed at exit. See the atexit
module.
For example, this will call print('goodbye.')
when exiting:
atexit.register(print, 'goodbye.')
You can also register any other function which will do the cleanup you need.

zvone
- 18,045
- 3
- 49
- 77
-
-
@OussemaNehdi It's in the standard library. You don't need to install it; it comes with Python. – nog642 Jul 12 '20 at 21:34
-
-