Skip to content

Instantly share code, notes, and snippets.

@mlbright
Created September 25, 2010 01:47
Show Gist options
  • Save mlbright/596359 to your computer and use it in GitHub Desktop.
Save mlbright/596359 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# ---------------------------------------------------------------------------- #
import sys
import os
import errno
import stat
import shutil
def _on_access_error(func, path, excinfo):
""" Error handler for shutil.rmtree.
If the error is due to an access error (read only file)
it attempts to add write permission and then retries.
If the error is for another reason it re-raises the error."""
if func in (os.rmdir, os.remove) and \
(excinfo[1].errno == errno.EACCES or not os.access(path,os.W_OK)):
# The path is not writable
os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
func(path)
print "... exception type %s" % (str(excinfo[0]))
print "... function %s" % (str(func))
print "... path %s" % (path)
else:
print "The error is some kind of other problem ..."
print str(func)
raise
def rmtree(dir):
if sys.platform == 'win32':
shutil.rmtree(dir,ignore_errors=False,onerror=_on_access_error)
else:
shutil.rmtree(dir)
if __name__ == "__main__":
rmtree(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment