I am using the function .to_excel()
in pandas,
import pandas as pd
df = pd.DataFrame([[1, 2], [3, 4]])
with pd.ExcelWriter('output.xlsx') as writer:
df.to_excel(writer, sheet_name="MySheet")
This results in the error OSError: [Errno 9] Bad file descriptor
The excel file already exists, but is not open elsewhere.
I am using python 3.10.11, pandas 2.2.3, openpyxl 3.1.5
Traceback:
with pd.ExcelWriter('output.xlsx') as writer:
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py:1353 in __exit__
self.close()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py:1357 in close
self._save()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_openpyxl.py:110 in _save
self.book.save(self._handles.handle)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\workbook\workbook.py:386 in save
save_workbook(self, filename)
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\writer\excel.py:294 in save_workbook
writer.save()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\writer\excel.py:275 in save
self.write_data()
File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\openpyxl\writer\excel.py:60 in write_data
archive.writestr(ARC_APP, tostring(props.to_tree()))
File ~\AppData\Local\Programs\Python\Python310\lib\zipfile.py:1816 in writestr
with self.open(zinfo, mode='w') as dest:
File ~\AppData\Local\Programs\Python\Python310\lib\zipfile.py:1180 in close
self._fileobj.seek(self._zinfo.header_offset)
OSError: [Errno 9] Bad file descriptor
The file location was synchronized with OneDrive.
Using another location that is not synchronized solved the issue.