How to execute code when a Python script is closed out? -
i have raspberry pi records temperature , stores them in mysql database on website. toy script, hitting ctrl+c on running script , re executing it. issue close() on database connection. how can run line of code when script exited in python?
import mysqldb con = mysqldb.connect(...) cursor = con.cursor() # ... #if script closes: #con.close()
import mysqldb con = mysqldb.connect(...) cursor = con.cursor() try: # stuff db finally: con.close() the finally clause executed on success on error (exception).
if hit ctrl-c, keyboardinterrupt exception.
Comments
Post a Comment