diff options
author | Guido van Rossum <guido@python.org> | 2003-05-13 18:01:19 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2003-05-13 18:01:19 (GMT) |
commit | bf1bef820c5af6b0a9a60abe1564ac35f036fdcb (patch) | |
tree | 28ff811dc3667e9efb3d021270c4a547f35c30c2 /Lib/os.py | |
parent | a2f7728341956963c6088c7ba5076da0b9c1e6dd (diff) | |
download | cpython-bf1bef820c5af6b0a9a60abe1564ac35f036fdcb.zip cpython-bf1bef820c5af6b0a9a60abe1564ac35f036fdcb.tar.gz cpython-bf1bef820c5af6b0a9a60abe1564ac35f036fdcb.tar.bz2 |
Add optional 'onerror' argument to os.walk(), to control error
handling.
Diffstat (limited to 'Lib/os.py')
-rw-r--r-- | Lib/os.py | 15 |
1 files changed, 12 insertions, 3 deletions
@@ -203,7 +203,7 @@ def renames(old, new): __all__.extend(["makedirs", "removedirs", "renames"]) -def walk(top, topdown=True): +def walk(top, topdown=True, onerror=None): """Directory tree generator. For each directory in the directory tree rooted at top (including top @@ -232,6 +232,13 @@ def walk(top, topdown=True): dirnames have already been generated by the time dirnames itself is generated. + By default errors from the os.listdir() call are ignored. If + optional arg 'onerror' is specified, it should be a function; it + will be called with one argument, an os.error instance. It can + report the error to continue with the walk, or raise the exception + to abort the walk. Note that the filename is available as the + filename attribute of the exception object. + Caution: if you pass a relative pathname for top, don't change the current working directory between resumptions of walk. walk never changes the current directory, and assumes that the client doesn't @@ -259,7 +266,9 @@ def walk(top, topdown=True): # Note that listdir and error are globals in this module due # to earlier import-*. names = listdir(top) - except error: + except error, err: + if onerror is not None: + onerror(err) return dirs, nondirs = [], [] @@ -274,7 +283,7 @@ def walk(top, topdown=True): for name in dirs: path = join(top, name) if not islink(path): - for x in walk(path, topdown): + for x in walk(path, topdown, onerror): yield x if not topdown: yield top, dirs, nondirs |