From f356fe95ba22381e8d799764bb921355127c5344 Mon Sep 17 00:00:00 2001 From: CookSleep <151028412+CookSleep@users.noreply.github.com> Date: Fri, 31 May 2024 01:02:36 +0800 Subject: [PATCH] Add files via upload --- main.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index 9285245..40b6037 100644 --- a/main.py +++ b/main.py @@ -63,19 +63,24 @@ 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\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("\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) @@ -83,6 +88,9 @@ def get_directory_tree(self, root_dir): 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"