Nodes/ComfyUI-Get-Random-File/Random File Path 🎲
ComfyUI Node

Random File Path 🎲

The 'grab me a random file' node people actually recommend

By ChrisColeTechΒ·Created 2 years agoΒ·Updated 4 days agoΒ· 3
Random File Path 🎲
    • filename
    β—„directory_pathβ–Ί
    β—„randomizetrueβ–Ί

    Give it a folder, and it hands you back the full path of a random file inside it. That's the whole node, and it's one of those two-line utilities that quietly solves a recurring problem: "load a random something from this directory" comes up constantly in ComfyUI, and this is the tidy one-stop answer.

    The name is accurate, so let's be clear about what it is not: it doesn't read the file, load an image, or call any API. It returns a plain STRING - the path. What you do with that path is up to the rest of your graph. On r/comfyui this is actually the recommended answer to "how do I load a random text file from a folder?" - you drop this node in, run its output into a Read Text File node, and suddenly every queue is pulling a different prompt, script line, or caption from your library. Random prompts without wildcard syntax, basically.

    How it works

    Under the hood it's boring in the best way: os.walk() crawls the directory recursively, collecting every file it finds, and random.choice() picks one. Two details matter. First, there's no extension filter - a random file really means any file, so that thumbs.db or .DS_Store is in the pool. Second, the node overrides IS_CHANGED to return float("NaN"), the standard ComfyUI idiom for "always re-run me." NaN is never equal to itself, so the cache can't decide the output is unchanged, and you get a fresh random file every time you hit Queue - not every time you look at the workflow. The cost is real, though: an always-dirty node defeats the cache for everything downstream of it, so if your graph re-runs when you'd expect it to skip, this node is why.

    The input and output that matter

    One input, one output, nothing hidden:

    • directory_path - an absolute path to a folder. Type it or wire in a string; it's not a ComfyUI file-picker. An empty or bogus path throws NotADirectoryError with the message "'...' is not a valid directory path."
    • Output - a single STRING: the full path of the chosen file. Run it into any node that accepts a file path - a Read Text File, a load-from-path loader, a script runner, whatever you've got.

    Installing it

    The pack is tiny (MIT, a handful of stars, no models to download), so install is painless. In ComfyUI Manager, search "ComfyUI-Get-Random-File" and hit install, or clone it:

    cd ComfyUI/custom_nodes
    git clone https://github.com/ChrisColeTech/ComfyUI-Get-Random-File
    

    Then restart ComfyUI. Manager will pull in the pack's requirements.txt (opencv-python and imageio-ffmpeg - those really exist for the pack's video nodes, so this node's own file-picking runs on the Python standard library alone).

    Gotchas

    • Recursive by default - subfolders count, so "files in this directory" includes everything nested inside it.
    • No filter - if you only want images or videos, use the sibling nodes Random Image Path or Random Video Path from the same pack instead.
    • Always re-runs - that NaN trick is exactly what you want here, but remember it costs cache-efficiency downstream.

    Given how little it does, there isn't much to break. Type the right absolute path, and it just works.

    CategoryπŸ€– CCTech/Files

    Inputs (2)

    NameTypeDefaultDescription
    directory_pathSTRINGβ€”
    randomizeoptBOOLEANtrueOn (default) = pick a new random file every run. Off = keep returning the currently selected file (per node; re-picks only if that file no longer exists).

    Outputs (1)

    NameTypeDescription
    filenameSTRINGβ€”