If you are using shutil.rmtree, you can use the onerror member of that function to provide a function that takes three params: function, path, and exception info. You can use this method to mark read only files as writable while you are deleting your tree.
import os, shutil, stat
def on_rm_error( func, path, exc_info):
# path contains the path of the file that couldn't be removed
# let's just assume that it's read-only and unlink it.
os.chmod( path, stat.S_IWRITE )
os.unlink( path )
shutil.rmtree( TEST_OBJECTS_DIR, onerror = on_rm_error )
Now, to be fair, the error function could be called for a variety of reasons. The 'func' parameter can tell you what function "failed" (os.rmdir() or os.remove()). What you do here depends on how bullet proof you want your rmtree to be. If it's really just a case of needing to mark files as writable, you could do what I did above. If you want to be more careful (i.e. determining if the directory coudln't be removed, or if there was a sharing violation on the file while trying to delete it), the appropriate logic would have to be inserted into the on_rm_error() function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…