From 333ebde5a2c48bfd4b33c89e84211c8c454cde3c Mon Sep 17 00:00:00 2001 From: StableLlama Date: Thu, 27 Aug 2026 21:53:17 +0200 Subject: [PATCH] Allow appending for text string saving --- src/basic_data_handling/path_nodes.py | 11 ++++++++--- tests/test_path_nodes.py | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/basic_data_handling/path_nodes.py b/src/basic_data_handling/path_nodes.py index 9274bea..078b33a 100644 --- a/src/basic_data_handling/path_nodes.py +++ b/src/basic_data_handling/path_nodes.py @@ -958,6 +958,8 @@ class PathSaveStringFile(ComfyNodeABC): This node takes a string and saves it to the specified path as a text file. Optionally, you can choose to create the directory if it doesn't exist. + If 'append' is True, the text is appended to an existing file instead of + overwriting it. """ @classmethod def INPUT_TYPES(cls): @@ -968,6 +970,7 @@ class PathSaveStringFile(ComfyNodeABC): }, "optional": { "create_dirs": (IO.BOOLEAN, {"default": True}), + "append": (IO.BOOLEAN, {"default": False}), "encoding": (IO.STRING, {"default": "utf-8"}), } } @@ -979,7 +982,7 @@ class PathSaveStringFile(ComfyNodeABC): FUNCTION = "save_text" OUTPUT_NODE = True - def save_text(self, text: str, path: str, create_dirs: bool = True, encoding: str = "utf-8"): + def save_text(self, text: str, path: str, create_dirs: bool = True, append: bool = False, encoding: str = "utf-8"): if not path: print("Basic data handling: Save failed - no path specified") return (False,) @@ -990,10 +993,12 @@ class PathSaveStringFile(ComfyNodeABC): if directory and create_dirs and not os.path.exists(directory): os.makedirs(directory) - with open(path, "w", encoding=encoding) as f: + mode = "a" if append else "w" + with open(path, mode, encoding=encoding) as f: f.write(text) - print(f"Basic data handling: Successfully saved text to {path}") + action = "appended" if append else "saved" + print(f"Basic data handling: Successfully {action} text to {path}") return (True,) except Exception as e: print(f"Basic data handling: Error saving text file: {e}") diff --git a/tests/test_path_nodes.py b/tests/test_path_nodes.py index 7516d70..65b447d 100644 --- a/tests/test_path_nodes.py +++ b/tests/test_path_nodes.py @@ -57,6 +57,25 @@ def test_path_load_save_string_file(tmp_path): # Test error handling assert load_node.load_text(str(tmp_path / "nonexistent.txt")) == ("", False) +def test_path_save_string_file_append(tmp_path): + # Test appending to an existing file + save_node = PathSaveStringFile() + load_node = PathLoadStringFile() + file_path = str(tmp_path / "append_test.txt") + + # Save initial content + assert save_node.save_text("First line\n", file_path) == (True,) + + # Append to the file + assert save_node.save_text("Second line\n", file_path, append=True) == (True,) + + # Verify the content was appended, not overwritten + assert load_node.load_text(file_path) == ("First line\nSecond line\n", True) + + # Verify default behavior still overwrites + assert save_node.save_text("Overwritten", file_path) == (True,) + assert load_node.load_text(file_path) == ("Overwritten", True) + def test_path_abspath(): node = PathAbspath()