ComfyUI Node

dirname

The folder half of any path

By StableLlama·Created about a year ago·Updated 4 days ago· 48
dirname
    • dirname
    path

    Every path has a folder side and a filename side. dirname is the folder side. Feed /home/you/ComfyUI/models/loras/my_style.safetensors into the dirname node from Basic data handling and you get /home/you/ComfyUI/models/loras - everything except the filename.

    It's the exact complement of basename (same pack, keeps the filename instead), and the pair is how you split a path into its two halves without any string-splitting hacks. If you've ever needed "save the output next to the file I loaded" or "derive the folder this model lives in," this is the node.

    How it works

    A wrapper around Python's os.path.dirname(). One input, one output:

    • path - STRING, default empty.
    • dirname - STRING output, the directory portion.

    It's separator-aware across Windows and Linux, so a path typed with either slash style resolves correctly on whichever OS you're running.

    The two gotchas

    • No folder in the path returns an empty string. dirname("my_style.safetensors") returns "" - a bare filename has no directory component. That's correct, but it means wiring that into a path-join without checking can produce a subtly broken path. If your inputs might be bare filenames, handle the empty case.
    • Trailing separators change the answer. dirname("/home/you/models/") returns /home/you/models - Python strips the trailing slash and treats the path as a directory. Meanwhile dirname("/home/you/models") (no slash) returns /home/you. Subtle, but it's the same rule basename has, so the two stay consistent with each other.

    Where you'd use it

    • "Same folder as the input" logic - derive where a loaded file lives, then place a saved file alongside it.
    • Model-folder inspection - given a checkpoint path, show or branch on which directory it came from.
    • Pairing with basename - split any path into its two halves and reassemble with the pack's PathJoin node if you need to swap the filename while keeping the folder.

    Installing it

    Basic data handling is a zero-dependency pack - no models, no pip installs, nothing to break. ComfyUI Manager → search "Basic data handling" → install → restart. Or:

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

    Restart, and dirname is under Basic/Path next to basename and abspath.

    The take

    dirname is unglamorous, but it's one half of the most useful pairing in the pack's Path section. If you're doing any file-location logic at all - and pretty much every save/load workflow is file-location logic - you'll end up with this node in your graph. Learn its empty-string behavior early and it'll never bite you.

    CategoryBasic/Path

    Inputs (1)

    NameTypeDefaultDescription
    pathSTRING

    Outputs (1)

    NameTypeDescription
    dirnameSTRING