File size: 2,661 Bytes
d712cef | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | from pathlib import Path
def print_directory_tree(start_path, allowed_extensions=('.py', '.tsx', '.db')):
"""
Prints a directory tree structure for a single path, filtered by extensions.
"""
start_path = Path(start_path).resolve()
# Check if the folder actually exists before trying to scan it
if not start_path.exists() or not start_path.is_dir():
print(f"β Error: Directory not found or is not a folder -> {start_path}")
return
# Helper function to check if a directory contains any relevant files
def has_relevant_files(dir_path):
for ext in allowed_extensions:
try:
next(dir_path.rglob(f"*{ext}"))
return True
except StopIteration:
continue
return False
def _print_tree(current_path, prefix=""):
entries = [e for e in current_path.iterdir() if not e.name.startswith('.')]
entries.sort(key=lambda x: (x.is_file(), x.name.lower()))
valid_entries = []
for entry in entries:
if entry.is_dir():
if has_relevant_files(entry):
valid_entries.append(entry)
elif entry.is_file() and entry.suffix in allowed_extensions:
valid_entries.append(entry)
for i, entry in enumerate(valid_entries):
is_last = (i == len(valid_entries) - 1)
connector = "βββ " if is_last else "βββ "
print(f"{prefix}{connector}{entry.name}{'/' if entry.is_dir() else ''}")
if entry.is_dir():
extension_prefix = " " if is_last else "β "
_print_tree(entry, prefix=prefix + extension_prefix)
print(f"π {start_path.name}/")
_print_tree(start_path)
def process_multiple_folders(folder_list, allowed_extensions=('.py', '.tsx', '.db')):
"""
Loops through a list of folders and prints the tree for each one.
"""
for folder in folder_list:
print(f"\n{'=' * 60}")
print(f"π² Tree Structure for: {folder}")
print(f"{'=' * 60}")
print_directory_tree(folder, allowed_extensions)
if __name__ == "__main__":
# π― Provide your list of folder paths here
# You can use absolute paths (C:/... or /Users/...) or relative paths (./...)
folders_to_scan = [
"AgenticControl", # Scans the current directory
"backend", # Example of a relative path
"frontend", # Example of an absolute path (Windows)
"Excel_Generator"
]
process_multiple_folders(folders_to_scan) |