Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
CookSleep authored May 30, 2024
1 parent d2c55be commit f356fe9
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,26 +63,34 @@ def dropEvent(self, event):

def generate_file_structure(self, root_dir, output_file):
with open(output_file, 'w', encoding='utf-8') as f:
f.write(self.get_directory_tree(root_dir))
f.write(self.get_directory_tree(root_dir, output_file))
for dirpath, _, filenames in os.walk(root_dir):
for filename in filenames:
filepath = os.path.join(dirpath, filename)
f.write(f"### {filepath} ###\n")
# 忽略输出文件
if filepath == output_file:
continue
normalized_path = os.path.normpath(filepath).replace('\\', '/')
f.write(f"\n<file path=\"{normalized_path}\">\n")
try:
with open(filepath, 'r', encoding='utf-8') as file:
content = file.read()
f.write(content + "\n\n")
f.write(content + "\n")
except Exception as e:
f.write(f"无法读取文件内容: {e}\n\n")
f.write(f"无法读取文件内容: {e}\n")
f.write("</file>\n")

def get_directory_tree(self, root_dir):
def get_directory_tree(self, root_dir, output_file):
tree = []
for dirpath, dirnames, filenames in os.walk(root_dir):
level = dirpath.replace(root_dir, '').count(os.sep)
indent = '│ ' * (level)
tree.append(f"{indent}├── {os.path.basename(dirpath)}/")
subindent = '│ ' * (level + 1)
for f in filenames:
# 忽略输出文件
if os.path.join(dirpath, f) == output_file:
continue
tree.append(f"{subindent}├── {f}")
return "\n".join(tree) + "\n"

Expand Down

0 comments on commit f356fe9

Please sign in to comment.