@@ -63,26 +63,34 @@ def dropEvent(self, event):
63
63
64
64
def generate_file_structure (self , root_dir , output_file ):
65
65
with open (output_file , 'w' , encoding = 'utf-8' ) as f :
66
- f .write (self .get_directory_tree (root_dir ))
66
+ f .write (self .get_directory_tree (root_dir , output_file ))
67
67
for dirpath , _ , filenames in os .walk (root_dir ):
68
68
for filename in filenames :
69
69
filepath = os .path .join (dirpath , filename )
70
- f .write (f"### { filepath } ###\n " )
70
+ # 忽略输出文件
71
+ if filepath == output_file :
72
+ continue
73
+ normalized_path = os .path .normpath (filepath ).replace ('\\ ' , '/' )
74
+ f .write (f"\n <file path=\" { normalized_path } \" >\n " )
71
75
try :
72
76
with open (filepath , 'r' , encoding = 'utf-8' ) as file :
73
77
content = file .read ()
74
- f .write (content + "\n \n " )
78
+ f .write (content + "\n " )
75
79
except Exception as e :
76
- f .write (f"无法读取文件内容: { e } \n \n " )
80
+ f .write (f"无法读取文件内容: { e } \n " )
81
+ f .write ("</file>\n " )
77
82
78
- def get_directory_tree (self , root_dir ):
83
+ def get_directory_tree (self , root_dir , output_file ):
79
84
tree = []
80
85
for dirpath , dirnames , filenames in os .walk (root_dir ):
81
86
level = dirpath .replace (root_dir , '' ).count (os .sep )
82
87
indent = '│ ' * (level )
83
88
tree .append (f"{ indent } ├── { os .path .basename (dirpath )} /" )
84
89
subindent = '│ ' * (level + 1 )
85
90
for f in filenames :
91
+ # 忽略输出文件
92
+ if os .path .join (dirpath , f ) == output_file :
93
+ continue
86
94
tree .append (f"{ subindent } ├── { f } " )
87
95
return "\n " .join (tree ) + "\n "
88
96
0 commit comments