Spaces:
Runtime error
Runtime error
Upload addknowledge.py
Browse files- test/addknowledge.py +46 -0
test/addknowledge.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
def read_file_content(file_path):
|
4 |
+
"""Чтение содержимого файла"""
|
5 |
+
with open(file_path, 'r', encoding='utf-8') as file:
|
6 |
+
return file.read()
|
7 |
+
|
8 |
+
def create_knowledge_file(root_dir, output_file):
|
9 |
+
"""Создание файла знаний с содержимым всех файлов проекта и визуализацией структуры директории"""
|
10 |
+
with open(output_file, 'w', encoding='utf-8') as knowledge_file:
|
11 |
+
# Визуализация структуры директории
|
12 |
+
knowledge_file.write("Структура директории проекта:\n")
|
13 |
+
visualize_directory_structure(root_dir, knowledge_file)
|
14 |
+
|
15 |
+
# Запись содержимого файлов
|
16 |
+
for root, _, files in os.walk(root_dir):
|
17 |
+
for file_name in files:
|
18 |
+
file_path = os.path.join(root, file_name)
|
19 |
+
knowledge_file.write(f"\n# Начало содержимого файла: {file_path}\n")
|
20 |
+
try:
|
21 |
+
content = read_file_content(file_path)
|
22 |
+
knowledge_file.write(content)
|
23 |
+
except Exception as e:
|
24 |
+
knowledge_file.write(f"Ошибка чтения файла {file_path}: {e}\n")
|
25 |
+
knowledge_file.write(f"# Конец содержимого файла: {file_path}\n")
|
26 |
+
|
27 |
+
def visualize_directory_structure(dir_path, knowledge_file, indent_level=0):
|
28 |
+
"""Визуализация структуры директории"""
|
29 |
+
items = os.listdir(dir_path)
|
30 |
+
for item in items:
|
31 |
+
item_path = os.path.join(dir_path, item)
|
32 |
+
if item_path == __file__:
|
33 |
+
continue # Исключаем текущий файл скрипта
|
34 |
+
knowledge_file.write(' ' * indent_level + '|-- ' + item + '\n')
|
35 |
+
if os.path.isdir(item_path):
|
36 |
+
visualize_directory_structure(item_path, knowledge_file, indent_level + 1)
|
37 |
+
|
38 |
+
def main():
|
39 |
+
root_dir = os.getcwd() # Текущая директория проекта
|
40 |
+
output_file = 'knowledge.txt'
|
41 |
+
|
42 |
+
create_knowledge_file(root_dir, output_file)
|
43 |
+
print(f"Файл знаний успешно создан: {output_file}")
|
44 |
+
|
45 |
+
if __name__ == '__main__':
|
46 |
+
main()
|