本文目录导读:

是的,完全可以!下面是几个实用的脚本,可以生成文件目录树,覆盖不同场景:
Python脚本(最通用)
import os
import sys
from pathlib import Path
def generate_tree(directory, prefix="", max_depth=None, current_depth=0):
"""
生成目录树结构
:param directory: 目录路径
:param prefix: 前缀字符串
:param max_depth: 最大深度限制
:param current_depth: 当前深度
"""
if max_depth is not None and current_depth > max_depth:
return
try:
items = sorted(os.listdir(directory))
except PermissionError:
print(f"{prefix}└── [权限不足]")
return
items = [item for item in items if not item.startswith('.')] # 忽略隐藏文件
for i, item in enumerate(items):
path = os.path.join(directory, item)
is_last = i == len(items) - 1
# 当前项的前缀
current_prefix = "└── " if is_last else "├── "
if os.path.isdir(path):
print(f"{prefix}{current_prefix}{item}/")
next_prefix = " " if is_last else "│ "
generate_tree(path, prefix + next_prefix, max_depth, current_depth + 1)
else:
print(f"{prefix}{current_prefix}{item}")
if __name__ == "__main__":
# 用法:python script.py [目录路径] [最大深度]
target_dir = sys.argv[1] if len(sys.argv) > 1 else "."
max_depth = int(sys.argv[2]) if len(sys.argv) > 2 else None
print(f"目录树: {target_dir}")
print(f"深度限制: {'无限制' if max_depth is None else max_depth}")
print()
print(target_dir + "/")
generate_tree(target_dir, max_depth=max_depth)
使用方法:
python tree_generator.py /path/to/directory 3 # 限制深度为3
Bash脚本(Linux/Mac)
#!/bin/bash
# 目录树生成脚本
# 用法: ./tree.sh [目录路径] [最大深度]
TARGET_DIR="${1:-.}"
MAX_DEPTH="${2:-}"
# 显示目录树(使用系统tree命令或替代方案)
if command -v tree &> /dev/null; then
# 系统有tree命令
if [ -n "$MAX_DEPTH" ]; then
tree -L "$MAX_DEPTH" "$TARGET_DIR"
else
tree "$TARGET_DIR"
fi
else
# 没有tree命令,使用find实现简易版本
echo "使用find替代tree命令..."
if [ -n "$MAX_DEPTH" ]; then
find "$TARGET_DIR" -maxdepth "$MAX_DEPTH" -not -path '*/\.*' | \
sort | sed 's|[^/]*/| |g' | sed 's| |├── |'
else
find "$TARGET_DIR" -not -path '*/\.*' | \
sort | sed 's|[^/]*/| |g' | sed 's| |├── |'
fi
fi
使用方法:
chmod +x tree.sh ./tree.sh /path/to/directory 2
PowerShell脚本(Windows)
# generate-tree.ps1
param(
[string]$Path = ".",
[int]$MaxDepth = -1,
[switch]$ShowHidden
)
function Show-Tree {
param(
[string]$CurrentPath,
[string]$Prefix = "",
[int]$CurrentDepth = 0,
[int]$MaxDepth = -1,
[bool]$ShowHidden = $false
)
if ($MaxDepth -ge 0 -and $CurrentDepth -gt $MaxDepth) {
return
}
try {
$items = Get-ChildItem -Path $CurrentPath -ErrorAction Stop
}
catch {
Write-Host "$Prefix└── [访问被拒绝: $CurrentPath]"
return
}
# 过滤隐藏文件
if (-not $ShowHidden) {
$items = $items | Where-Object { -not $_.PSIsContainer -or -not $_.Name.StartsWith('.') }
$items = $items | Where-Object { -not $_.Name.StartsWith('.') }
}
$itemArray = @($items | Sort-Object Name)
for ($i = 0; $i -lt $itemArray.Length; $i++) {
$item = $itemArray[$i]
$isLast = ($i -eq $itemArray.Length - 1)
$currentPrefix = if ($isLast) { "└── " } else { "├── " }
if ($item.PSIsContainer) {
Write-Host "$Prefix$currentPrefix$($item.Name)/"
$nextPrefix = if ($isLast) { " " } else { "│ " }
Show-Tree -CurrentPath $item.FullName -Prefix ($Prefix + $nextPrefix) `
-CurrentDepth ($CurrentDepth + 1) -MaxDepth $MaxDepth -ShowHidden $ShowHidden
}
else {
$size = "{0:N0}KB" -f ($item.Length / 1KB)
Write-Host "$Prefix$currentPrefix$($item.Name) ($size)"
}
}
}
# 主程序
$resolvedPath = Resolve-Path $Path -ErrorAction Stop
Write-Host "目录树: $resolvedPath"
if ($MaxDepth -ge 0) {
Write-Host "最大深度: $MaxDepth"
}
Write-Host "`n$resolvedPath/"
Show-Tree -CurrentPath $resolvedPath -MaxDepth $MaxDepth -ShowHidden $ShowHidden
使用方法:
.\generate-tree.ps1 -Path "C:\MyFolder" -MaxDepth 3
排除特定目录的版本
# 带有排除功能的Python版本
def generate_tree_advanced(directory, prefix="", max_depth=None, exclude_dirs=None):
"""
高级目录树生成,可以排除特定目录
"""
if exclude_dirs is None:
exclude_dirs = {'node_modules', '.git', '__pycache__', '.venv', 'venv'}
if max_depth is not None and current_depth > max_depth:
return
try:
items = [f for f in os.listdir(directory) if not f.startswith('.')]
items = [f for f in items if f not in exclude_dirs]
items.sort()
except PermissionError:
return
for i, item in enumerate(items):
path = os.path.join(directory, item)
is_last = i == len(items) - 1
if os.path.isdir(path):
print(f"{prefix}{'└── ' if is_last else '├── '}{item}/")
next_prefix = " " if is_last else "│ "
generate_tree_advanced(path, prefix + next_prefix, max_depth,
exclude_dirs, current_depth + 1)
else:
print(f"{prefix}{'└── ' if is_last else '├── '}{item}")
输出到文件的版本
#!/bin/bash
# 将目录树输出到文件
TARGET_DIR="${1:-.}"
OUTPUT_FILE="${2:-directory_tree.txt}"
if command -v tree &> /dev/null; then
tree "$TARGET_DIR" > "$OUTPUT_FILE"
else
find "$TARGET_DIR" -type f -o -type d | sort > "$OUTPUT_FILE"
fi
echo "目录树已保存到: $OUTPUT_FILE"
推荐使用场景:
- Python脚本:跨平台,功能最完整,可定制性强
- Bash脚本:Linux/Mac用户最方便
- PowerShell:Windows用户最佳选择
- 系统tree命令:如果系统已安装,最简单直接
这些脚本可以帮你快速生成项目目录结构,用于文档、项目说明或代码审查。
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播