w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to execute code when a Python script is closed out?
import MySQLdb
con = MySQLdb.connect(...)
cursor = con.cursor() 

try:
    # do stuff with your DB    
finally:
    con.close()

The finally clause is executed on success as well as on error (exception).

If you hit Ctrl-C, you get a KeyboardInterrupt exception.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.