def force_removal(func, path, excinfo): ''' This is the only way to ensure that readonly files are deleted by rmtree on Windows. See: http://bugs.python.org/issue19643 ''' # Due to the way 'onerror' is implemented in shutil.rmtree, errors # encountered while listing directories cannot be recovered from. So if # a directory cannot be listed, shutil.rmtree assumes that it is empty # and it tries to call os.remove() on it which fails. This is just one # way in which this can fail, so for robustness we just call 'rm' if we # get an OSError while trying to remove a specific path. # See: http://bugs.python.org/issue8523 try: os.chmod(path, stat.S_IWRITE) func(path) except OSError: shell_call('rm -rf ' + path)