Iterate files recursively

suggest change

To iterate all files, including those in subdirectories, use os.walk. root_dir can be "." to start from the current directory, or any other path:

import os
for root, dirs, files in os.walk(root_dir):
    for filename in files:
        print(os.path.join(root, filename))

os.scandir lists a single directory and can tell you whether each entry is a file without a separate stat call:

import os
for entry in os.scandir(path):
   if not entry.name.startswith('.') and entry.is_file():
       print(entry.name)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents