user18673048
user18673048

Reputation: 11

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

I am trying to run a python script I inherited. I'm not a programmer, but know enough to understand. The problem is that this script works perfectly in my older machine, but still 64bit machine with Office 365 - Excel.

I keep getting the error when I migrated to a new machine. I need some assistance how to fix it so it works on the new machine. Below is the error and part of the script where it is failing.

PermissionError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_16060/1139512431.py in 121 122 #Delete unneeded xlsx file --> 123 os.remove("C:\MarketCentre Deals Report\Data Files\MarketCentreDealsReport_West_"+datetime.datetime.now().strftime("%m.%d.%Y")+".xlsx") 124 125 ##########################################################################################################################

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\MarketCentre Deals Report\Data Files\MarketCentreDealsReport_West_03.07.2022.xlsx'


Code where it is erroring:

 #Resave final file as xlsb to reduce file size
excel = win32com.client.Dispatch("Excel.Application")
doc = excel.Workbooks.Open("C:\\MarketCentre Deals Report\\Data Files\\MarketCentreDealsReport_East_"+datetime.datetime.now().strftime("%m.%d.%Y")+".xlsx")
doc.SaveAs( "C:\\MarketCentre Deals Report\\Data Files\\MarketCentreDealsReport_East_"+datetime.datetime.now().strftime("%m.%d.%Y")+".xlsb", 50 )

Upvotes: 1

Views: 3066

Answers (2)

Lukas Kucinski
Lukas Kucinski

Reputation: 1

I ran into the same issue. Uninstall Anaconda, restarting, and reinstalling fixed it.

Upvotes: 0

Yennefer
Yennefer

Reputation: 6234

The error you are receiving is due to the fact that the file is locked. Since you have migrated from a different machine, there can be a number of factors that can affect the new code, ranging from subtle runtime changes (very infrequent but can happen if you change the major version too) to different software interaction on the new machine.

The big offender, is usually the antimalware software which could be a little bit slow in scanning the file, while scanning the file is locked exclusively on purpose to prevent accesses to file of dubious nature).

Or simply, the workflow that used to work on the previous machine no longer works on this. In order to investigate, start by using Process Explorer as explained in How to find out what processes have folder or file locked? answer and you should be able to track the offending process.

If that does not solve the problem, you can use ProcMon to capture whatever happens on the machine in the precise moment your request fails. You may have a look at The Ultimate Guide to Procmon for furher documentation and help on the tool.

Upvotes: 0

Related Questions