| 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() |
| |
| |
| 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 |
|
|
| |
| 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__": |
| |
| |
| folders_to_scan = [ |
| "AgenticControl", |
| "backend", |
| "frontend", |
| "Excel_Generator" |
| ] |
| |
| process_multiple_folders(folders_to_scan) |