-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_projectstructure.py
More file actions
57 lines (49 loc) · 2.12 KB
/
Copy pathprint_projectstructure.py
File metadata and controls
57 lines (49 loc) · 2.12 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
# %%
import os
# a. Plain style
def print_lines(startpath, output_file, ignore_dirs=None, ignore_files=None):
if ignore_dirs is None:
ignore_dirs = []
if ignore_files is None:
ignore_files = []
with open(output_file, 'w', encoding='utf-8') as f:
for root, dirs, files in os.walk(startpath):
# Filter out ignored directories and files
dirs[:] = [d for d in dirs if d not in ignore_dirs]
files[:] = [f for f in files if f not in ignore_files]
# Sort for consistent output
dirs.sort()
files.sort()
level = root.replace(startpath, '').count(os.sep)
indent = ' ' * level
print(f"{indent}{os.path.basename(root)}/")
subindent = ' ' * (level + 1)
for f in files:
print(f"{subindent}{f}")
# b. A little prettier style
def print_tree(startpath, output_file, ignore_dirs=None, ignore_files=None):
if ignore_dirs is None:
ignore_dirs = []
if ignore_files is None:
ignore_files = []
with open(output_file, 'w', encoding='utf-8') as f:
for root, dirs, files in os.walk(startpath):
dirs[:] = [d for d in dirs if d not in ignore_dirs]
files[:] = [f for f in files if f not in ignore_files]
dirs.sort()
files.sort()
level = root.replace(startpath, '').count(os.sep)
indent = '│ ' * level
branch = '├── ' if level > 0 else ''
f.write(f"{indent}{branch}{os.path.basename(root)}/\n")
for i, file in enumerate(files):
subindent = '│ ' * (level + 1)
connector = '└── ' if i == len(files) - 1 else '├── '
f.write(f"{subindent}{connector}{file}\n")
# Example usage
project_root = os.path.abspath('.')
output_txt = os.path.join(project_root, 'project_structure.txt')
folders_toignore = ['.git', '.Rproj.user', '.venv']
files_toignore = ['.gitignore', 'git_project1.Rproj']
print_tree(project_root, output_txt, ignore_dirs=folders_toignore, ignore_files=files_toignore)
# End