删除Python中的只读目录 [英] Deleting read-only directory in Python
本文介绍了删除Python中的只读目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
shutil.rmtree
不会删除 Windows 上的只读文件.是否有相当于rm -rf"的python?为什么啊,为什么这么痛?
shutil.rmtree
will not delete read-only files on Windows. Is there a python equivalent of "rm -rf" ? Why oh why is this such a pain?
推荐答案
shutil.rmtree
可以采用错误处理函数,当删除文件出现问题时将调用该函数.您可以使用它来强制删除有问题的文件.
shutil.rmtree
can take an error-handling function that will be called when it has problem removing a file. You can use that to force the removal of the problematic file(s).
灵感来自 http://mail.python.org/pipermail/Tutor/2006-June/047551.html 和 http://techarttiki.blogspot.com/2008/08/read-only-windows-files-with-python.html:
import os
import stat
import shutil
def remove_readonly(func, path, excinfo):
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(top, onerror=remove_readonly)
(我还没有测试过那个片段,但它应该足以让你开始)
(I haven't tested that snippet out, but it should be enough to get you started)
这篇关于删除Python中的只读目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文