mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Added rmtree(), to recursively remove a directory tree.
Code by David Ascher (docstring by me).
This commit is contained in:
parent
a0fec2b5df
commit
d76732918a
1 changed files with 32 additions and 0 deletions
|
@ -93,3 +93,35 @@ def copytree(src, dst, symlinks=0):
|
||||||
# XXX What about devices, sockets etc.?
|
# XXX What about devices, sockets etc.?
|
||||||
except (IOError, os.error), why:
|
except (IOError, os.error), why:
|
||||||
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
|
print "Can't copy %s to %s: %s" % (`srcname`, `dstname`, str(why))
|
||||||
|
|
||||||
|
def rmtree(path, ignore_errors=0, onerror=None):
|
||||||
|
"""Recursively delete a directory tree.
|
||||||
|
|
||||||
|
If ignore_errors is set, errors are ignored; otherwise, if
|
||||||
|
onerror is set, it is called to handle the error; otherwise, an
|
||||||
|
exception is raised.
|
||||||
|
|
||||||
|
"""
|
||||||
|
cmdtuples = []
|
||||||
|
_build_cmdtuple(path, cmdtuples)
|
||||||
|
for cmd in cmdtuples:
|
||||||
|
try:
|
||||||
|
apply(cmd[0], (cmd[1],))
|
||||||
|
except:
|
||||||
|
exc = sys.exc_info()
|
||||||
|
if ignore_errors:
|
||||||
|
pass
|
||||||
|
elif onerror:
|
||||||
|
onerror(cmd[0], cmd[1], exc)
|
||||||
|
else:
|
||||||
|
raise exc[0], (exc[1][0], exc[1][1] + ' removing '+cmd[1])
|
||||||
|
|
||||||
|
# Helper for rmtree()
|
||||||
|
def _build_cmdtuple(path, cmdtuples):
|
||||||
|
for f in os.listdir(path):
|
||||||
|
real_f = os.path.join(path,f)
|
||||||
|
if os.path.isdir(real_f) and not os.path.islink(real_f):
|
||||||
|
_build_cmdtuple(real_f, cmdtuples)
|
||||||
|
else:
|
||||||
|
cmdtuples.append(os.remove, real_f)
|
||||||
|
cmdtuples.append(os.rmdir, path)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue