2010-04-16 129 views
49

在Python中,包含只读文件的文件夹在运行时shutil.rmtree,以下异常印:shutil.rmtree在Windows上失败,“访问被拒绝”

File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 216, in rmtree 
    rmtree(fullname, ignore_errors, onerror) 
File "C:\Python26\lib\shutil.py", line 221, in rmtree 
    onerror(os.remove, fullname, sys.exc_info()) 
File "C:\Python26\lib\shutil.py", line 219, in rmtree 
    os.remove(fullname) 
WindowsError: [Error 5] Access is denied: 'build\\tcl\\tcl8.5\\msgs\\af.msg' 

在文件属性对话框展望我注意到af.msg文件被设置为只读。

所以,问题是:什么是最简单的解决办法 /修复来解决这个问题 - 因为我的本意是做的rm -rf build/但在Windows等效? (无需使用第三方工具,如unxutils或Cygwin的 - 因为这代码是针对要在裸露的Windows上运行的Python 2.6安装瓦特/安装PyWin32)

+4

'shutil.rmtree'使用'os.remove'来删除文件。 'os.remove'删除只读文件就好了(至少在Unix上)。如果正在使用,“os.remove”无法删除Windows上的文件。 – jfs 2010-04-16 22:24:04

+0

[在Python中删除目录]可能的重复(http://stackoverflow.com/questions/1889597/deleting-directory-in-python) – mozzbozz 2015-01-21 16:20:58

回答

63

检查这个问题了:

What user do python scripts run as in windows?

显然,答案是将文件/文件夹更改为不可读取,然后将其删除。

下面是pathutils.pyonerror()处理器通过@Sridhar Ratnakumar在评论中提到:

def onerror(func, path, exc_info): 
    """ 
    Error handler for ``shutil.rmtree``. 

    If the error is due to an access error (read only file) 
    it attempts to add write permission and then retries. 

    If the error is for another reason it re-raises the error. 

    Usage : ``shutil.rmtree(path, onerror=onerror)`` 
    """ 
    import stat 
    if not os.access(path, os.W_OK): 
     # Is the error an access error ? 
     os.chmod(path, stat.S_IWUSR) 
     func(path) 
    else: 
     raise 
+1

heh。我刚刚在http://www.voidspace.org.uk/downloads/pathutils.py – 2010-04-16 22:27:33

+0

发现了'onerror'处理程序..发现通过http://trac.pythonpaste.org/pythonpaste/ticket/359 – 2010-04-16 22:33:57

+1

即使此答案状态的评论'将文件/文件夹更改为不可读',但我仍然收到只读文件夹上的拒绝访问权限。尽管如此,[这](http://stackoverflow.com/a/1889686/116047)实现工作。 – Pakman 2013-11-13 17:50:36

17

我会说有os.walk确保试图删除它之前对每个文件使用os.chmod访问实现自己的rmtree。

像这样(未经):

import os 
import stat 

def rmtree(top): 
    for root, dirs, files in os.walk(top, topdown=False): 
     for name in files: 
      filename = os.path.join(root, name) 
      os.chmod(filename, stat.S_IWUSR) 
      os.remove(filename) 
     for name in dirs: 
      os.rmdir(os.path.join(root, name)) 
    os.rmdir(top)  
+0

这几乎是正确的 - Windows只支持'stat.S_IWRITE'(这正是你想要的) - http:// docs。 python.org/library/os.html#os.chmod – 2010-04-16 22:25:26

+0

我测试了'os.chmod(filename,stat.S_IWUSR)'去掉了只读标志,所以它在WinXP上工作。考虑到这是文档关于'stat.S_IWRITE'的说法:“S_IWUSR的Unix V7同义词”(http://docs.python.org/library/stat.html#stat.S_IWRITE),我在想我的代码反正是对的。 – Epcylon 2010-04-17 13:27:33

+0

太棒了,文件路径太长,这似乎是唯一的方法。也许建议承诺或改变shutil.rmtree。 – Anthony 2015-04-10 00:03:16

5

好了,明显的解决方案并没有为我工作...这样做,而不是:

os.system('rmdir /S /Q "{}"'.format(directory)) 
0
shutil.rmtree(path,ignore_errors=False,onerror=errorRemoveReadonly) 
def errorRemoveReadonly(func, path, exc): 
    excvalue = exc[1] 
    if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES: 
     # change the file to be readable,writable,executable: 0777 
     os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO) 
     # retry 
     func(path) 
    else: 
     raiseenter code here 

如果ignore_errors是设置,错误被忽略;否则,如果设置了错误 ,则调用它来处理带有参数(func, path,exc_info)的错误,其中func是os.listdir,os.remove或os.rmdir; 路径是导致它失败的那个函数的参数;和 exc_info是由sys.exc_info()返回的元组。如果ignore_errors 是假的,是的onerror无,一个例外是这里

1

raised.enter代码简单的解决方法是使用subprocess.call

from subprocess import call 
call("rm -rf build/", shell=True) 

为了执行你想要的一切。