diff -r 1a972140ab62 Lib/os.py --- a/Lib/os.py Thu Mar 12 09:12:48 2015 +0100 +++ b/Lib/os.py Thu Mar 12 10:04:15 2015 +0100 @@ -354,7 +354,7 @@ def walk(top, topdown=True, onerror=None dirs = [] nondirs = [] - symlinks = set() + symlinks = {} # We may not have read permission for top, in which case we can't # get a list of the files the directory contains. os.walk @@ -379,13 +379,12 @@ def walk(top, topdown=True, onerror=None if is_dir and not followlinks: try: - if entry.is_symlink(): - symlinks.add(entry.name) + symlinks[entry.name] = entry.is_symlink() except OSError: # If is_symlink() raises an OSError, consider that the # entry is not a symbolik link, same behaviour than # os.path.islink(). - pass + symlinks[entry.name] = False except OSError as error: # scandir() or iterating into scandir() iterator raised an OSError if onerror is not None: @@ -398,7 +397,15 @@ def walk(top, topdown=True, onerror=None # Recurse into sub-directories for name in dirs: - if followlinks or name not in symlinks: + if followlinks: + walk_into = True + else: + try: + walk_into = not symlinks[name] + except KeyError: + # directory added after dirs was created + walk_into = not path.islink(path.join(top, name)) + if walk_into: new_path = path.join(top, name) yield from walk(new_path, topdown, onerror, followlinks)