删除Python中的目录 [英] Deleting directory in Python
本文介绍了删除Python中的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
shutil.rmtree
不会在Windows上删除只读文件。有没有相当于python的rm -rf?为什么啊为什么这么痛苦?
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屋!
查看全文