-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
read_excel with openpyxl engine doesn't close file after reading #29803
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The class |
Is this fixed by #30096? |
the fix creates a new problem, if I pass in a file object, it closes it automatically... maybe need to have a check on whether it's a file object when passed in |
Same here |
This cannot be reproduced anymore with the latest pandas master. import psutil
import pandas as pd
p = psutil.Process()
print(p.open_files())
print('Reading excel file...')
data = pd.read_excel('some_file.xlsx', engine='openpyxl')
print('File has been read')
print(p.open_files()) No left over open file handles are detected. The output is this and means no open file handles before and after the test:
|
I have this issue persisiting with following I am loading a dataframe into pandas using following:
The dataframe loads just fine but the file is left open:
I am running Anaconda/Jupyter). Relevant versions I am running:
I would appreciate some suggestions on how to close the files/best work around this, thanks |
I can still reliably duplicate this problem. I am on Windows using Pandas 1.3.5. Python version 3.10.0 In this code, the function get_excel() leaves the file locked while it is running (before you dismiss the messagebox). The function get_excel2 clears the lock on the file. I can use the get_excel2 code to fix the problem but it seems like fixing the root of the problem in Pandas read_excel would be a better solution.
|
@JeffryErickson does your example depend on using tkinter? |
@jbrockmendel No it doesn't depend on using tkinter. When I convert the messagebox lines to print statements and comment out tkinter, psutil.Process() reports the spreadsheet is open the second time it runs before it gets to a closing print() function. |
@JeffryErickson In
|
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:
Output:
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):I'm not sure if it's a normal behavior of
ExcelReader
, but I expectpd.read_excel()
to close the file in this case.openpyxl: 3.0.0
The text was updated successfully, but these errors were encountered: