fwalk(top='.', topdown=True, onerror=None, *, follow_symlinks=False, dir_fd=None)

Directory tree generator.

This behaves exactly like walk(), except that it yields a 4-tuple

dirpath, dirnames, filenames, dirfd

dirpath, dirnames and filenames are identical to walk() output, and dirfd is a file descriptor referring to the directory dirpath.

The advantage of fwalk() over walk() is that it’s safe against symlink races (when follow_symlinks is False).

If dir_fd is not None, it should be a file descriptor open to a directory,

and top should be relative; top will then be relative to that directory. (dir_fd is always supported for fwalk.)

Caution: Since fwalk() yields file descriptors, those are only valid until the next iteration step, so you should dup() them if you want to keep them for a longer period.


import os for root, dirs, files, rootfd in os.fwalk(‘python/Lib/email’):

print(root, “consumes”, end=”“) print(sum([os.stat(name, dir_fd=rootfd).st_size for name in files]),


print(“bytes in”, len(files), “non-directory files”) if ‘CVS’ in dirs:

dirs.remove(‘CVS’) # don’t visit CVS directories