summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorTim Golden <mail@timgolden.me.uk>2014-05-07 17:05:45 (GMT)
committerTim Golden <mail@timgolden.me.uk>2014-05-07 17:05:45 (GMT)
commit783377998a5139544f5c429ce621c38ceb674adf (patch)
tree7fb121978115e59bd1f577304b3974bb5f26c9c3 /Doc
parent4ce74dc54cbb4baea01e028260bc2e0cd86f67e5 (diff)
downloadcpython-783377998a5139544f5c429ce621c38ceb674adf.zip
cpython-783377998a5139544f5c429ce621c38ceb674adf.tar.gz
cpython-783377998a5139544f5c429ce621c38ceb674adf.tar.bz2
Issue19643 Add an example of shutil.rmtree which shows how to cope with readonly files on Windows
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/shutil.rst20
1 files changed, 20 insertions, 0 deletions
diff --git a/Doc/library/shutil.rst b/Doc/library/shutil.rst
index e4f348c..eb81c7d 100644
--- a/Doc/library/shutil.rst
+++ b/Doc/library/shutil.rst
@@ -421,6 +421,26 @@ Another example that uses the *ignore* argument to add a logging call::
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