-
-
Notifications
You must be signed in to change notification settings - Fork 19.4k
Open
Labels
Description
import pandas as pd
data = pd.read_excel('excel_file.xlsx', engine='openpyxl')Problem description:
After running the above code in console, I switch to Excel and try to save 'excel_file.xlsx'. Excel raises an access error.
I tried to figure out if the file remains open using file descriptor. As output shows, it does:
import os
import pandas as pd
xl_file_stat = os.stat(os.path.relpath('excel_file.xlsx'))
try:
os.fstat(3)
except OSError:
print('File is not open')
print('Reading excel file...')
data = pd.read_excel('excel_file.xlsx', engine='openpyxl')
print('File has been read')
if os.fstat(3) == xl_file_stat:
print('File is open')Output:
File is not open
Reading excel file...
File has been read
File is open
The problem may arise from the openpyxl's class ExcelReader. Its' read() method closes the archive if read-only attribute is set to False (openpyxl/reader/excel.py, rows 281-282):
if not self.read_only:
self.archive.close()
I'm not sure if it's a normal behavior of ExcelReader, but I expect pd.read_excel() to close the file in this case.
pandas: 0.25.3
openpyxl: 3.0.0
openpyxl: 3.0.0