I am trying to catch an error when writing an excel file, for example when the file is already open:
import pandas as pd
import xlsxwriter
test = "2020/04/02|17:50:33|Conversion succeeded (SemanticProtocolConverter): TEST/Neuro/Neuro/Dot Tete VE11/AX T1 mprage post|TE: 3.24 --> 3.02 ms; Echo Spacing: 7.84 --> 7.62 ms; Coil Selection: Manual --> ACS All but spine|22808"
test2 = test.split("|")
df = pd.DataFrame(test2)
df = df.transpose()
outDF = test2
outXLSX = pd.ExcelWriter("test.xlsx", engine='xlsxwriter')
df.to_excel(outXLSX, 'Test', index=False)
try:
outXLSX.save()
except IOError:
print("Cannot open the file")
print("done")
The problem is that it doesn't catch the error. How can I make sure I can write to the file?
Thanks, Bart