擅长:python、mysql、java
<p><code>shutil.rmtree</code>可以使用一个错误处理函数,当它在删除文件时遇到问题时将调用该函数。您可以使用它强制删除有问题的文件。</p>
<p>灵感来自<a href="http://mail.python.org/pipermail/tutor/2006-June/047551.html" rel="noreferrer">http://mail.python.org/pipermail/tutor/2006-June/047551.html</a>和<a href="http://techarttiki.blogspot.com/2008/08/read-only-windows-files-with-python.html" rel="noreferrer">http://techarttiki.blogspot.com/2008/08/read-only-windows-files-with-python.html</a>:</p>
<pre><code>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)
</code></pre>
<p>(我还没有测试过这个片段,但它应该足以让你开始)</p>