PyDocs

Nav

Generic Operating System Services

fwalk

os

function · Added in 3.3 (source) Unix
  • 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.

Example:

import os
for root, dirs, files, rootfd in os.fwalk('python/Lib/xml'):
    print(root, "consumes", end="")
    print(sum(os.stat(name, dir_fd=rootfd).st_size for name in files),
          end="")
    print("bytes in", len(files), "non-directory files")
    if '__pycache__' in dirs:
        dirs.remove('__pycache__')  # don't visit __pycache__ directories
Changed in 3.6, 3.7
  • 3.6: Accepts a path-like object.
  • 3.7: Added support for bytes paths.
0 Examples
No examples for os/fwalk.
See Also
No see-alsos for os/fwalk
0 Notes
No notes for fwalk