File size: 1,135 Bytes
9a9b2ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

def afficher_arborescence(dossier: str = '..', niveau_max: int = 2, prefixe: str = '') -> None:
    """
    Affiche récursivement l'arborescence d'un dossier jusqu'à un certain niveau de profondeur.

    Paramètres
    ----------
    dossier : str
        Chemin du dossier racine à explorer.
    niveau_max : int
        Profondeur maximale de l'affichage (0 = seulement la racine).
    prefixe : str
        Préfixe utilisé pour formater l'affichage (géré automatiquement en récursif).
    """
    if niveau_max < 0:
        return
    try:
        fichiers = sorted(os.listdir(dossier))
    except PermissionError:
        print(f"{prefixe}📛 Accès refusé à {dossier}")
        return
    for fichier in fichiers:
        chemin = os.path.join(dossier, fichier)
        print(f"{prefixe}├── {fichier}")
        if os.path.isdir(chemin):
            afficher_arborescence(chemin, niveau_max - 1, prefixe + '│   ')

## - Utilisation de la fonction

# from src.utils.tree_utils import afficher_arborescence

## - Affiche 3 niveaux à partir du dossier courant
# afficher_arborescence('.', niveau_max=3)