basename
Stripping a path down to the filename
- basename
Every file path is two parts glued together: the folders that lead to the file, and the filename itself. The basename node from Basic data handling keeps the second part and throws away the first. Feed it /home/you/ComfyUI/models/loras/my_style.safetensors and it hands back just my_style.safetensors.
That sounds trivial until you're building a workflow that needs to name something after the file it loaded - a save filename, a display label, a prompt that should include the model's name - and you realize there's no clean stock node that just strips a path to its tail. This is that node.
How it works
A wrapper around Python's os.path.basename(), which is smarter than a naive "split on the separator and take the last piece." One input, one output:
path- STRING, default empty.basename- STRING output, the final path component.
It handles both slash directions (so paths from Windows and Linux both work on either OS), collapses trailing separators, and returns only the last component.
The two gotchas
- A trailing separator eats the basename.
basename("/home/you/models/")returns an empty string, because Python treats that path as "the directorymodels/, nothing after it." If your paths sometimes end in a slash (directories, not files), strip it first or expect"". This is the classicbasenamefootgun, and it's caught hundreds of people who've never even seen this node. - No directory = the whole string.
basename("my_style.safetensors")(no folders at all) returns the whole thing, which is probably what you want. Meanwhiledirname(its partner node) returns""in that same case - the two nodes are exact opposites, and that asymmetry is normal.
Where you'd use it
- Naming outputs - take a loaded checkpoint's path, extract the basename, and use it as a prefix for saved images so you always know what generated them.
- Displaying which model/lora a path refers to, without the folder noise.
- Comparing files by name rather than full path (pair it with
NotEqualfrom the same pack).
Installing it
Basic data handling is a zero-dependency pack - no models, no pip installs. 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 basename lives under Basic/Path next to dirname.
The take
basename is the kind of node you don't think about until you're assembling a filename from a path - and then it's the missing link. Pair it with dirname to split a path into its two halves, and you've got path surgery without a string-splitting nightmare.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| path | STRING | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| basename | STRING | — |