0

I created an excel file to read in some formula-linked values, and I wish to delete file at the end of the process but I get the below error:

PermissionError: [WinError 32] The process cannot access the file because it is being used by another proces

I have already closed the file prior to deletion. Can someone assist? Thank you

shutil.copy(file,'file2.xlsm')
file2=os.path.abspath('file2.xlsm')
 
wb2=openpyxl.load_workbook(file2, data_only=True,  read_only=True, keep_vba=True, keep_links=True)

*Code to read in values from wb2*

wb2.close()
os.remove(file2)
Unknown
  • 33
  • 8
  • 1
    Looks like a duplicate. See here https://stackoverflow.com/questions/27215462/permissionerror-winerror-32-the-process-cannot-access-the-file-because-it-is – SaaSy Monster Sep 25 '20 at 10:37
  • Have you checked whether another process maybe using the file? https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows – Bahroze Ali Sep 25 '20 at 10:38
  • Hi, The other post (https://stackoverflow.com/questions/9065063/cant-delete-file-process-cannot-access-the-file-because-it-is-being-used-by-an ) was about C++. This is NOT a duplicate post. Also I stated explicitly that I closed my file prior to deletion so there is NO other process using it. – Unknown Sep 25 '20 at 11:24
  • I find the users in this forum strange to deduct my reputation points when I am asking unique Qns with explicit details – Unknown Sep 25 '20 at 11:25
  • @Unknown did you try the link in the first comment? Similar scenario just a different file type. – SaaSy Monster Sep 25 '20 at 12:13
  • 1
    The problem is with the read_only mode of openpyxl and after running into the same problem and having checked all sources, I haven't found a solution, only a workaround. Please see [this answer](https://stackoverflow.com/a/45387809/14132438) for details. (I would have preferred to post this as a comment to your question, but I don't have enough reputation to do it.) – aed Sep 25 '20 at 14:02
  • Hi aed, your reply is most relevant and useful to my question. "wb._archive.close()" or "io.BytesIO" didn't work for me. I simply have to change to "read_only=False" – Unknown Sep 27 '20 at 04:43

0 Answers0