Skip to content Skip to sidebar Skip to footer

Taking The Signal From Close Event Of Terminal In Python

How can I make a python script that is run only through terminal (no GUI) to not quit when the red X at the top is pressed, but to assign a function to that signal which eventually

Solution 1:

In general, you can use the atexit module to register functions to be called on exit:

try:
    _count = int(open("/tmp/counter").read())
except IOError:
    _count = 0

def incrcounter(n):
    global _count
    _count = _count + n

def savecounter():
    open("/tmp/counter", "w").write("%d" % _count)

import atexit
atexit.register(savecounter)

Of course, the user can always force-quit your process, and you can't do anything about that!


Post a Comment for "Taking The Signal From Close Event Of Terminal In Python"