31 lines
1.1 KiB
Python
31 lines
1.1 KiB
Python
import os
|
|
|
|
def generate_tree(start_path='.', max_depth=2):
|
|
lines = []
|
|
|
|
def _tree(dir_path, depth, prefix):
|
|
if depth > max_depth:
|
|
return
|
|
entries = sorted(os.listdir(dir_path))
|
|
for i, entry in enumerate(entries):
|
|
path = os.path.join(dir_path, entry)
|
|
connector = "└── " if i == len(entries) - 1 else "├── "
|
|
lines.append(prefix + connector + entry)
|
|
if os.path.isdir(path):
|
|
extension = " " if i == len(entries) - 1 else "│ "
|
|
_tree(path, depth + 1, prefix + extension)
|
|
|
|
lines.append(start_path)
|
|
_tree(start_path, 1, '')
|
|
return '\n'.join(lines)
|
|
|
|
def save_tree_to_file(start_path='.', max_depth=2, output_file='tree.txt'):
|
|
tree_output = generate_tree(start_path, max_depth)
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|
f.write(tree_output)
|
|
print(f"Árbol guardado en: {output_file}")
|
|
|
|
# Ejemplo de uso:
|
|
# Puedes cambiar estos valores según lo necesites
|
|
save_tree_to_file(start_path=r'E:\Fitz_Studio', max_depth=3, output_file=r'E:\Fitz_Studio\data\files\txt\tree.txt')
|