-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathtree.py
More file actions
88 lines (71 loc) · 2.85 KB
/
tree.py
File metadata and controls
88 lines (71 loc) · 2.85 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"""
Generates a markdown file representing the project's file structure.
Usage:
python tree.py [root_directory] [output_filename]
"""
import pathlib
import sys
from typing import TextIO, List, Optional
def generate_file_structure(
root_dir: pathlib.Path,
file_handle: TextIO,
ignore_list: List[str],
prefix: str = "",
):
"""
Generates a tree-like structure for a directory and writes it to a file.
Args:
root_dir (pathlib.Path): The path to the root directory.
file_handle (TextIO): The file object to write the output to.
ignore_list (List[str]): A list of names to ignore.
prefix (str): The prefix for drawing the tree structure.
"""
try:
paths = sorted(
[p for p in root_dir.iterdir() if p.name not in ignore_list],
key=lambda p: (not p.is_dir(), p.name.lower()),
)
except FileNotFoundError:
print(f"Warning: Directory {root_dir} not found.", file=sys.stderr)
return
for i, path in enumerate(paths):
is_last = i == len(paths) - 1
connector = "└── " if is_last else "├── "
if path.is_dir():
print(f"{prefix}{connector}📁 {path.name}/", file=file_handle)
new_prefix = prefix + (" " if is_last else "│ ")
generate_file_structure(path, file_handle, ignore_list, new_prefix)
else:
print(f"{prefix}{connector}📄 {path.name}", file=file_handle)
if __name__ == "__main__":
# --- Configuration ---
DEFAULT_IGNORE = [
"__pycache__", ".git", ".github", ".vscode", "build",
"dist", ".mypy_cache", ".pytest_cache", "venv", ".venv"
]
# The directory to scan (defaults to current directory)
start_path_str = sys.argv[1] if len(sys.argv) > 1 else "."
# The output filename (defaults to STRUCTURE.md)
output_filename = sys.argv[2] if len(sys.argv) > 2 else "STRUCTURE.md"
# --- Execution ---
start_path = pathlib.Path(start_path_str)
root_name = start_path.resolve().name
if not start_path.is_dir():
print(f"Error: '{start_path_str}' is not a valid directory.")
sys.exit(1)
try:
with open(output_filename, "w", encoding="utf-8") as f:
# Write the Markdown header and opening code block
f.write("# Project File Structure\n\n```\n")
# Write the root directory and start the tree
print(f"📁 {root_name}/", file=f)
generate_file_structure(
start_path,
file_handle=f,
ignore_list=DEFAULT_IGNORE
)
# Write the closing code block
f.write("```\n")
print(f"✅ File structure successfully saved to '{output_filename}'")
except IOError as e:
print(f"Error writing to file: {e}")