1

I want to capture command window close event from Python.
In other words, when the user tries to close the command prompt window, script should detect it and display a message like Do you really want to exit - Yes/No

Any suggestions about how to implement this? Please help me in doing this.

TheCodeArtist
  • 20,467
  • 4
  • 64
  • 128
hari123
  • 11
  • 1
  • 2

1 Answers1

4

define like this:

import time

def on_exit(sig, func=None):
    print("exit handler")
    time.sleep(10)  # so you can see the message before program exits
  • Windows:

if you install pywin32 package, you can :

import win32api
win32api.SetConsoleCtrlHandler(on_exit, True)
  • Un*x:

Or, using python internal "signal" library, if you are using under *nix system:

import signal
signal.signal(signal.SIGTERM, on_exit)
Jean-François Fabre
  • 131,796
  • 23
  • 122
  • 195
winterTTr
  • 1,721
  • 1
  • 9
  • 30