Skip to content

text.paths

join paths and take them apart: folder, file name, extension

A path is text like /home/ada/notes.txt. These functions join paths and take them apart without touching the disk, so they work on paths that don’t exist.

Tags: parsing

  • path_join join pieces into one path, with / or \ between them as your system uses
  • basename the last part of a path, usually the file name, or nil
  • dirname everything before the last part of a path, usually its folder, or nil
  • ext the file’s extension without the dot, like “gz”, or nil if it has none
  • stem the file name without its last extension, or nil
path_join(a: str, *more: str) -> str

join pieces into one path, with / or \ between them as your system uses

1
path_join("a", "b.txt")"a/b.txt"
Example outputCtrl+Enter to run

See also dirname, basename

basename(path: str) -> str|nil

the last part of a path, usually the file name, or nil

1
"a/b/c.txt".basename"c.txt"
Example outputCtrl+Enter to run

See also dirname, stem

dirname(path: str) -> str|nil

everything before the last part of a path, usually its folder, or nil

1
"a/b/c.txt".dirname"a/b"
Example outputCtrl+Enter to run

See also basename

ext(path: str) -> str|nil

the file’s extension without the dot, like “gz”, or nil if it has none

1
"a/b.tar.gz".ext"gz"
2
"README".extnil
Example outputCtrl+Enter to run

See also stem

stem(path: str) -> str|nil

the file name without its last extension, or nil

1
"a/b.tar.gz".stem"b.tar"
Example outputCtrl+Enter to run

See also ext, basename

1
# a file name without, and then only, its extension
2
["a/b.tar.gz".stem, "a/b.tar.gz".ext]["b.tar", "gz"]
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.