[Python-checkins] cpython: Issue19643 Add an example of shutil.rmtree which shows how to cope with

tim.golden python-checkins at python.org
Wed May 7 19:08:14 CEST 2014


http://hg.python.org/cpython/rev/31d63ea5dffa
changeset:   90583:31d63ea5dffa
user:        Tim Golden <mail at timgolden.me.uk>
date:        Wed May 07 18:05:45 2014 +0100
summary:
  Issue19643 Add an example of shutil.rmtree which shows how to cope with readonly files on Windows

files:
  Doc/library/shutil.rst |  20 ++++++++++++++++++++
  1 files changed, 20 insertions(+), 0 deletions(-)


diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -421,6 +421,26 @@
    copytree(source, destination, ignore=_logpath)
 
 
+.. _shutil-rmtree-example:
+
+rmtree example
+~~~~~~~~~~~~~~
+
+This example shows how to remove a directory tree on Windows where some
+of the files have their read-only bit set. It uses the onerror callback
+to clear the readonly bit and reattempt the remove. Any subsequent failure
+will propagate. ::
+
+    import os, stat
+    import shutil
+    
+    def remove_readonly(func, path, _):
+        "Clear the readonly bit and reattempt the removal"
+        os.chmod(path, stat.S_IWRITE)
+        func(path)   
+   
+    shutil.rmtree(directory, onerror=remove_readonly)
+
 .. _archiving-operations:
 
 Archiving operations

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list