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

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -