The following code successfully creates individual files, except the last file which has to be opened in recovery mode. I've tried looking into all possible issues with no luck.
QUESTION: Based on the following code, is there any reason why most of the files are created correctly while the last file is corrupt?
for key, value in sorted(mdm_sheet_to_df_map.items()):
mdm_group = value.groupby('MDM')
for MDM, group_df in mdm_group:
attachment_mdm = attachment_path_mdm / f'{MDM}' / f'Q221 New Accounts - {MDM} - {today_string1}.xlsx'
attachment1_mdm = os.makedirs(os.path.dirname(attachment_mdm), exist_ok=True)
try:
writer = ExcelWriter(attachment_mdm, engine = 'openpyxl', mode="a")
except FileNotFoundError:
writer = ExcelWriter(attachment_mdm, engine = 'openpyxl')
group_df.to_excel(writer, sheet_name =f'{key}', index=False)
writer.save()
writer.close()
EDIT: There are no exceptions/errors raised in Python when the script is run. The issue is found when I try to open the last file which is in recovery. All other files open normally.