1

I'm making a function that deleats a directory if it already existed, then no matter if the dierctory already existed or not, it creates it afterwards.

The fist time I run the code, it works well, but if I try running it again, the following error pops: [WinError 32] The process does not have access to the file because it is being used by another process. I do not have anything else open so I don´t know where the error might be coming from

I´m using Windows 10 and Python 3.8.5

import os
import shutil

Path_1 = "D:/felip/Redes_Neuronales_Team/Redes_Neuronales_Pruebas/Prueba_del_programa/DirA"
Path_2 = "D:/felip/Redes_Neuronales_Team/Redes_Neuronales_Pruebas/Prueba_del_programa/DirB"

#For the 1st Directory

if os.path.exists(DirA):
    shutil.rmtree(DirA)
        
#For the 2nd Directory

if os.path.exists(DirB):
    shutil.rmtree(DirB)
   
os.mkdir(DirA)
os.mkdir(DirB)       
FAZ
  • 31
  • 3

1 Answers1

0

You can try to find the process using that folder then kill it. Find out which process is locking a file or folder in Windows

dinhanhx
  • 198
  • 2
  • 13
  • Is it any other way to do it directly form Python? I don´t love the idea of searching for the process and killing it manualy everytime. Thanks for your answer – FAZ Feb 14 '21 at 04:31
  • I think there is a way [Check if a file is not open nor being used by another process](https://stackoverflow.com/a/11115521/13358358) – dinhanhx Feb 14 '21 at 04:36