Last active
August 29, 2015 14:01
-
-
Save dmiro/199c189a1cd8fb250b42 to your computer and use it in GitHub Desktop.
Python atexit.register example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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