def file_tree(path, level=0, prefix=""):
    # 상위 폴더를 출력
    if level == 0:
        print(f"{path}/")
        
    # os.walk를 통해 폴더를 순차적으로 탐색
    for root, dirs, files in os.walk(path):
        # 현재 디렉토리의 상대 경로에서 깊이를 구함.
        depth = root.replace(path, '').count(os.sep)
        
        # 디렉토리 깊이에 맞는 들여쓰기 - 각 디렉토리 및 파일의 앞에 추가할 들여쓰기를 만들기 위한 문자열
        indent = "│   " * (depth - 1) + "└── " if depth > 0 else ""
        
        # 현재 디렉토리 출력
        if depth > 0:
            print(f"{indent}{os.path.basename(root)}/")     # ,basename : /home/user/docs라는 경로에서 docs만 반환
            
        # 하위 디렉토리 출력 (재귀적으로 호출) - for 문은 dirs 리스트에 포함된 모든 하위 디렉토리를 하나씩 처리
        for dir_name in dirs:
            print(f"{indent}│   ├── {dir_name}/")
        
        # 하위 파일이 있으면 파일도 출력
        for file_name in files:
            print(f"{indent}│   └── {file_name}")
            

file_tree(('./review-sentiment-analysis'))

'📘 Programming > Python' 카테고리의 다른 글

[tensorflow] tf.argmax()  (0) 2025.05.16

+ Recent posts