Skip to content

FIND FILES BY EXTENSION

Python
#!/usr/bin/env python3
"""FIND FILES BY EXTENSION"""
import os, tempfile
temp = tempfile.gettempdir()
test_dir = os.path.join(temp, "find_test")
os.makedirs(test_dir, exist_ok=True)
os.makedirs(os.path.join(test_dir, "subdir"))
files = ["test.txt", "script.py", "data.csv", "subdir/nested.py"]
for f in files:
    open(os.path.join(test_dir, f), 'w').write("test")
print("Finding .py files:")
for root, dirs, files in os.walk(test_dir):
    for file in files:
        if file.endswith('.py'):
            full_path = os.path.join(root, file)
            rel_path = os.path.relpath(full_path, test_dir)
            print(f"  {rel_path}")
import shutil
shutil.rmtree(test_dir)