Skip to content

Instantly share code, notes, and snippets.

@dmiro
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save dmiro/199c189a1cd8fb250b42 to your computer and use it in GitHub Desktop.

Select an option

Save dmiro/199c189a1cd8fb250b42 to your computer and use it in GitHub Desktop.
Python atexit.register example
"""
atexit.register example
The atexit module defines a single function to register cleanup functions.
Functions thus registered are automatically executed upon normal interpreter termination
https://docs.python.org/2/library/atexit.html
"""
# Caution: If you work with IDLE this example doesn't work
try:
_count = int(open("c:\counter").read())
except IOError:
_count = 0
def incrcounter(n):
global _count
_count = _count + n
def savecounter():
open("c:\counter", "w").write("%d" % _count)
import atexit, sys
atexit.register(savecounter)
incrcounter(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment