Python 3删除目录时出错[WinError 32]进程无法访问该文件,因为另一个进程正在使用该文件

2024-05-14 21:42:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我在做一个函数,如果目录已经存在,就删除它,然后不管目录是否已经存在,它都会在以后创建它

我第一次运行代码时,它运行得很好,但是如果我再次尝试运行它,会弹出以下错误:[WinError 32]该进程无法访问该文件,因为它正被另一个进程使用。 我没有打开任何其他文件,因此我不知道错误可能来自何处

我正在使用Windows10和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)       

Tags: 文件pathimport目录进程os错误team

热门问题