项目路径文本生成

PHOTO EMBED

Fri Aug 23 2024 03:27:59 GMT+0000 (Coordinated Universal Time)

Saved by @theshyxin #undefined

​import os

def generate_structure_string(start_path, exclude_dirs=None):
    if exclude_dirs is None:
        exclude_dirs = []

    structure = []
    for root, dirs, files in os.walk(start_path):
        # 跳过需要排除的目录
        if any(excluded in root for excluded in exclude_dirs):
            continue

        level = root.replace(start_path, '').count(os.sep)
        indent = '│   ' * level + '├── ' if level > 0 else ''
        sub_indent = '│   ' * (level + 1) + '├── '
        structure.append(f'{indent}{os.path.basename(root)}/')

        for f in files:
            structure.append(f'{sub_indent}{f}')

    return '\n'.join(structure)

if __name__ == "__main__":
    start_path = '.'  # 你的项目根目录路径
    exclude_dirs = ['static', '__pycache__', '.git']  # 需要排除的文件夹列表
    print(generate_structure_string(start_path, exclude_dirs))
content_copyCOPY