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() [...]
in case want other things well, try ... except ... else ... finally
statement(s) need. see documentation
Comments
Post a Comment