Iterate files recursively
suggest changeTo 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)
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents