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, 2023 in Others by Kithuzzz
• 38,000 points
7,865 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, 2023 by narikkadan
• 63,600 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
• 63,600 points
5,507 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, 2023 in Others by narikkadan
• 63,600 points
1,430 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 4,454 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
• 63,600 points
1,462 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
• 63,600 points
611 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