March Docs

Path

Path module: pure path manipulation (no I/O). All operations are pure string functions.

Functions

fnassert_no_traversalassert_no_traversal(path)#

Panic if path contains any .. component (path traversal attempt). Call this before joining a user-supplied path segment into a filesystem path.

Examples

    march> Path.assert_no_traversal("foo/bar")
    ()
    march> Path.assert_no_traversal("../etc/passwd")
    ** panic: path traversal detected: ../etc/passwd
fnbasenamebasename(path)#

Return the last component of a path, or the path itself when there are no components (empty string, single segment with no /).

Examples

    march> Path.basename("/usr/local/bin/march")
    "march"
    march> Path.basename("file.txt")
    "file.txt"
    march> Path.basename("/")
    "/"
fncomponentscomponents(path)#

Split a path into its non-empty /-separated components. Repeated separators and a leading / collapse to no extra entries.

Examples

    march> Path.components("/usr/local/bin")
    ["usr", "local", "bin"]
    march> Path.components("a//b/")
    ["a", "b"]
    march> Path.components("")
    []
fndirnamedirname(path)#

Return the directory portion of a path: everything except the last component. Returns "/" for an absolute path with one component and "." for a relative path with one component, mirroring the POSIX convention.

Examples

    march> Path.dirname("/usr/local/bin/march")
    "/usr/local/bin"
    march> Path.dirname("/etc")
    "/"
    march> Path.dirname("file.txt")
    "."
fnextensionextension(path)#

Return the file extension (without the leading dot), or the empty string if there is none. A leading . does not count as an extension (so .bashrc has no extension).

Examples

    march> Path.extension("file.txt")
    "txt"
    march> Path.extension("archive.tar.gz")
    "gz"
    march> Path.extension(".bashrc")
    ""
    march> Path.extension("README")
    ""
fnis_absoluteis_absolute(path)#

Returns true if path starts with /.

Examples

    march> Path.is_absolute("/etc")
    true
    march> Path.is_absolute("etc")
    false
    march> Path.is_absolute("")
    false
fnjoinjoin(a, b)#

Join two path segments with a single separator, avoiding double slashes if a already ends with /.

Examples

    march> Path.join("/usr", "local")
    "/usr/local"
    march> Path.join("/usr/", "local")
    "/usr/local"
    march> Path.join("a", "b")
    "a/b"
fnjoin_alljoin_all(parts)#

Join a list of path segments into a single path, inserting / separators. Respects a trailing / on any segment (avoids doubling). An empty list returns the empty string.

Examples

    march> Path.join_all(["usr", "local", "bin"])
    "usr/local/bin"
    march> Path.join_all(["/var/store", "packages", "ab", "cd"])
    "/var/store/packages/ab/cd"
    march> Path.join_all([])
    ""
fnnormalizenormalize(path)#

Normalize a path by collapsing . (current dir) and .. (parent) segments. Absolute paths cannot ascend past root; relative paths preserve leading .. components. An empty input or a path that cancels out to nothing returns ".".

Examples

    march> Path.normalize("/usr/local/../bin")
    "/usr/bin"
    march> Path.normalize("a/./b/../c")
    "a/c"
    march> Path.normalize("../foo/../bar")
    "../bar"
    march> Path.normalize("")
    "."
    march> Path.normalize("/..")
    "/"
fnstrip_extensionstrip_extension(path)#

Remove the final extension from a path, preserving the directory. Files with no extension (or only a leading dot) are returned unchanged.

Examples

    march> Path.strip_extension("file.txt")
    "file"
    march> Path.strip_extension("/tmp/archive.tar.gz")
    "/tmp/archive.tar"
    march> Path.strip_extension("README")
    "README"
    march> Path.strip_extension(".bashrc")
    ".bashrc"
fnstrip_leading_slashstrip_leading_slash(path)#

Remove a single leading / from a path, if present. A path that does not start with / is returned unchanged. An empty path is returned unchanged.

Examples

    march> Path.strip_leading_slash("/foo/bar")
    "foo/bar"
    march> Path.strip_leading_slash("foo/bar")
    "foo/bar"
    march> Path.strip_leading_slash("/")
    ""