shutil.rmtree 在Windows上出现“拒绝访问”的错误。

10 浏览
0 Comments

shutil.rmtree 在Windows上出现“拒绝访问”的错误。

在Python中,当在包含只读文件的文件夹上运行shutil.rmtree时,会打印出以下异常信息:

 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文件被设置为只读。那么问题是:如何以最简单的方式解决这个问题 - 假设我的意图是在Windows上执行类似于rm -rf build/的操作?(而不需要使用像unxutils或cygwin这样的第三方工具 - 因为这段代码的目标是在一个干净的安装了Python 2.6和PyWin32的Windows上运行)

0