想不想一键生成整个项目的目录树结构?

wen 实用脚本 45

本文目录导读:

想不想一键生成整个项目的目录树结构?

  1. 🔧 方法1:命令行原生工具(最通用)
  2. 🐍 方法2:Python脚本(可深度定制)
  3. 📦 方法3:专业工具(推荐)
  4. 🚀 方法4:IDE内置功能
  5. 🎯 最省心方案(我推荐)

当然可以!以下是几个快速生成项目目录树结构的方法,根据你的需求选择最合适的「一键方案」:

🔧 方法1:命令行原生工具(最通用)

Windows (CMD/PowerShell):

# 生成当前目录树
tree /F > tree.txt
# 排除node_modules等目录(需手动编辑)
dir /s /b | findstr /v node_modules

Mac/Linux:

# 基础版
tree > tree.txt
# 进阶排除(跳过node_modules, .git等)
tree -I 'node_modules|.git|__pycache__' > tree.txt

🐍 方法2:Python脚本(可深度定制)

# 项目结构生成器
import os
def gen_tree(path='.', prefix='', ignore_dirs=None):
    if ignore_dirs is None:
        ignore_dirs = {'.git', 'node_modules', '__pycache__', '.next'}
    items = sorted(os.listdir(path))
    for i, item in enumerate(items):
        if item in ignore_dirs:
            continue
        is_last = i == len(items) - 1
        connector = '└── ' if is_last else '├── '
        print(f'{prefix}{connector}{item}')
        full_path = os.path.join(path, item)
        if os.path.isdir(full_path):
            new_prefix = prefix + ('    ' if is_last else '│   ')
            gen_tree(full_path, new_prefix, ignore_dirs)
# 使用:
gen_tree('.')

📦 方法3:专业工具(推荐)

安装 tree-cli(Node.js)

npm install -g tree-cli
# 一键生成带描述的树
tree -L 3 --ignore 'node_modules,.git' -o structure.txt

macOS 用户:

brew install tree
tree -L 3 --charset utf-8

🚀 方法4:IDE内置功能

VS Code:

  1. 安装插件:Project Tree
  2. Ctrl+Shift+PGenerate Project Tree
  3. 自动生成带文件描述的目录树

JetBrains IDE:

  1. 右键项目 → Copy Path/ReferenceCopy Structure as text

🎯 最省心方案(我推荐)

直接使用这条跨平台命令(需要安装Node.js):

npx treer -e 'node_modules,.git,.next' > project-tree.txt

执行后你会得到一个整洁的:

project/
├── src/
│   ├── components/
│   │   ├── Header.jsx
│   │   └── Footer.jsx
│   ├── pages/
│   │   ├── index.js
│   │   └── about.js
│   └── styles/
│       └── global.css
├── public/
│   └── favicon.ico
├── package.json
└── README.md

需要哪个方法的完整实现? 我可以给你更详细的代码或解释!

抱歉,评论功能暂时关闭!