ID EN
OS

fwalk

Python 3.11

This behaves exactly like walk(), except that it yields a 4-tuple (dirpath, dirnames, filenames, dirfd), and it supports dir_fd.

Syntax

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

Examples

Example 1
PYTHON
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]),
          end="")
    print("bytes in", len(files), "non-directory files")
    if 'CVS' in dirs:
        dirs.remove('CVS')   don't visit CVS directories
Example 2
PYTHON
Delete everything reachable from the directory named in "top",
 assuming there are no symbolic links.
 CAUTION:  This is dangerous!  For example, if top == '/', it
 could delete all your disk files.
import os
for root, dirs, files, rootfd in os.fwalk(top, topdown=False):
    for name in files:
        os.unlink(name, dir_fd=rootfd)
    for name in dirs:
        os.rmdir(name, dir_fd=rootfd)

See Also

walk() walk() paths relative to directory descriptors not following symlinks fwalk() fwalk() dup() rmdir()