Overwrite directory with shutil.rmtree and os.mkdir sometimes gives 'Access is denied' error

那年仲夏 提交于 2020-05-14 18:23:08

问题


My code:

if os.path.exists(myDir):
    shutil.rmtree(myDir)
os.mkdir(myDir)

Problem: It always work if myDir does not exist. If myDir exists, sometimes it throws error, sometimes it works.

Error log:

os.mkdir(myDir)
PermissionError: [WinError 5] Access is denied: 'myDir'

My guess: when I call os.mkdir, sometimes shutil.rmtree hasn't finished execution/ hasn't released the permission for the directory. Hence, the error.

Is there any way to ensure complete execution of shutil.rmtree before calling os.mkdir?


回答1:


So I encountered the same issue. What I have been using is a pause after shutil.rmtree. I think that pretty much anything that causes your computer to use a clock cycle would do. All code:

import os 
import shutil 
import time

dataDir = 'C:/Data/'
if os.path.exists(TEMPDIR):
    shutil.rmtree(TEMPDIR)
time.sleep(.0000000000000001)
os.makedirs(TEMPDIR)



回答2:


If at first you don't succeed...

if os.path.exists(report_path):
    shutil.rmtree(report_path)
while True:
    try:
        os.mkdir(report_path)
        break
    except PermissionError:
        print('Damned Win 10 PERMISSION exception, trying again')
        continue

...and if that doesn't work there is at least <ctrl> -c




回答3:


I have been running into similar issues / the same error message on Windows 10 with calls to shutil.rmtree(dir) being denied access and subsequently not being executed. This in a primitive backup program I wrote which is designed to delete backups over a certain age. I am still testing the below but thus far it works as a temporary workaround in my setups however your mileage may vary.

Running the script as administrator didn't help either. Using os.chmod(f, S_IWRITE) [and/or stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IXUSR | stat.S_IRUSR | stat.S_IWUSR | stat.S_IWGRP | stat.S_IXGRP] also got the same refusal by the OS. Changing Controlled Folder Access (which I suspect may be causing this some of the time in Windows 10) and also modifying read only properties on the respective directories also did not help. Neither did changing ownership nor write access in the security tab help.

The workaround I use at the moment is to install, import and use the 'send2trash' module which can be used to remove the directory - assuming your program is otherwise sound - without said access error.

Thereafter one can delete the file from the recycle bin by hand or by automated means(after import of shell from win32com.shell) - obviously this second step will remove all files from the recycle bin. So absolutely check and think BEFORE you run this otherwise you may lose data you don't want to....).

I hope this is helpful.



来源:https://stackoverflow.com/questions/49809471/overwrite-directory-with-shutil-rmtree-and-os-mkdir-sometimes-gives-access-is-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!