Nodes/Basic data handling/save STRING to file
ComfyUI Node

save STRING to file

Get a string out of ComfyUI and into a real file

By StableLlama·Created about a year ago·Updated 4 days ago· 48
save STRING to file
    • success
    text
    path
    create_dirstrue
    appendfalse
    encodingutf-8

    ComfyUI is weirdly good at producing strings and weirdly bad at letting you have them. Prompts, caption files, JSON, workflow parameters - they all live inside the graph, and the default way to get one out is to copy-paste from a preview widget. This node ends that. You feed it a STRING and a path, and it writes a text file. That's the whole job, and for a surprising number of real workflows it's exactly what's missing.

    Think about what a string becomes useful outside the graph. A caption file sitting next to your training images for Kohya or ai-toolkit LoRA training. A prompt log for every image you generate, appended into a plain .txt you can grep later. A JSON payload for an automation script that's watching the output folder. None of that is possible with stock ComfyUI, and this is one of the few nodes that makes it boring instead of clever.

    How it works

    save_text is nearly a one-liner in the source: if create_dirs is on (it is by default), it runs os.makedirs on the parent directory so you don't have to pre-create it, then opens the file for writing with whatever encoding you gave (default utf-8) and writes the text. It's an output node, so it counts as a graph endpoint, and it returns success as a BOOLEAN so you can branch on whether the write actually happened.

    That last bit matters more than it looks. The node catches its own exceptions and prints Basic data handling: Error saving text file: ... to the console instead of crashing the workflow. So an unwritable path doesn't fail loudly - you get success = False and a log line. Wire success into a flow-control node if you want the workflow to stop when the file didn't land.

    The inputs that matter

    • text - the string you're writing. Wire this from any text-producing node.
    • path - the full destination, e.g. /data/output/captions/img_0001.txt. Empty path fails with False.
    • create_dirs - default true; turn off if you'd rather it error than silently make directories.
    • encoding - leave at utf-8 unless you have a real reason.

    Output is just the one success BOOLEAN.

    Installing it

    This ships in the Basic data handling pack by StableLlama. Either way is fine:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    

    then restart - or search "Basic data handling" in ComfyUI Manager and click install. The pack has zero dependencies and downloads no models, which is why it's a safe pick for the "what utility packs do I actually install" pile: it can't break your environment because it doesn't touch it.

    Gotchas

    • It overwrites - no append mode, no timestamping. If you want a running log, generate the full filename (with a Time node, say) so each write lands in its own file.
    • Parent directory creation only happens when create_dirs is true. Keep it on unless you've got a reason.
    • Encoding errors and permission errors both surface as success = False with a console line rather than a popup. When "nothing happened," that's where to look.
    CategoryBasic/Path

    Inputs (5)

    NameTypeDefaultDescription
    textSTRING
    pathSTRING
    create_dirsoptBOOLEANtrue
    appendoptBOOLEANfalse
    encodingoptSTRINGutf-8

    Outputs (1)

    NameTypeDescription
    successBOOLEAN