PermissionError WinError 32 The process cannot access the file because it is being used by another process Open Excel File in Python

0 votes

Regarding my problem, I require assistance "WinError 32: [PermissionError] The file is being used by another process, thus the process cannot access it: ".

Therefore, the script below deletes a folder that contains an excel file. When the excel file is opened, the shutil.rmtree(dirpath) command is not executed. Can someone point me in the direction of a solution that prompts the user when the file is opened? I look forward to hearing from you. I sincerely appreciate it in advance.

import os
import shutil

dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
   shutil.rmtree(dirpath)
   print('Deleted.')

else:
   print('Folder does not exist!')
   messagebox.showinfo('Ok.')
Jan 15 in Others by Kithuzzz
• 28,700 points
175 views

1 answer to this question.

0 votes

Try this:

import os
import shutil

dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
   try:
       os.rename(dirpath, dirpath)
       shutil.rmtree(dirpath)
       print('Deleted.')
   except:
       messagebox.showinfo('File opened by another process')

else:
   print('Folder does not exist!')
   messagebox.showinfo('Ok.')
answered Jan 15 by narikkadan
• 53,160 points

Related Questions In Others

0 votes
1 answer

PHPExcel file cannot open file because the file format or file extension is not valid

Just change the code to this: // Save Excel ...READ MORE

answered Nov 24, 2022 in Others by narikkadan
• 53,160 points
711 views
0 votes
1 answer

Blazor - ClosedXml - excel cannot open the file beacause the file format or extension is not valid

Changed the following code: function BlazorDownloadFile(filename, bytesBase64) { var ...READ MORE

answered Jan 20 in Others by narikkadan
• 53,160 points
58 views
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 3,323 views
0 votes
1 answer
0 votes
1 answer

Excel cannot open the file because the file format or file extension is not valid - PHP Maatwebsite

Try this: if (ob_get_length() == 0 ) { ...READ MORE

answered Dec 19, 2022 in Others by narikkadan
• 53,160 points
115 views
0 votes
1 answer

MAX function in Excel: is it possible to provide the range by means of variables?

Try this: =MAX(INDEX(A:A,B2):INDEX(A:A,B3)) READ MORE

answered Nov 15, 2022 in Others by narikkadan
• 53,160 points
62 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP