1

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?

  • 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 Answers1

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