Is there a way to feed a python script a command if it crashes at any point? -


i familiar try, except. i'm looking more general method runs simple command before script stops running after error raised.

for example if i'm using xlsxwriter:

import xlsxwriter wb = xlssxwriter.workbook ws = wb.add_worksheet() in range (len(list)):     ws.write(i,0,"hello world!")  #if script fails, run ws.close() 

this context managers (the with statement). workbook class provides necessary.

it simple as

with xlsxwriter.workbook('hello_world.xlsx') workbook:     worksheet = workbook.add_worksheet()     [...] 

see workbook documentation


in case want other things well, try ... except ... else ... finally statement(s) need. see documentation


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -