Skip to content
Docs
Modules
filepath

filepath

The filepath module contains utilities thare are used to manipulate file paths with operating system-specific separators.

Functions

abs

Function signature
abs(path string) string

Returns the absolute representation of path. If the path is not absolute it will be joined with the current working directory to create the corresponding absolute path.

Example
>>> filepath.abs("foo/bar")
"/home/user/foo/bar"
>>> filepath.abs("../foo/bar")
"/home/foo/bar"

Learn more: filepath.Abs (opens in a new tab).

base

Function signature
base(path string) string

Returns the last element of path with any trailing slashes removed. If path is empty, "." is returned.

Example
>>> filepath.base("foo/bar")
"bar"
>>> filepath.base("foo/bar/")
"bar"
>>> filepath.base("test.txt")
"test.txt"
>>> filepath.base("")
"."

Learn more: filepath.Base (opens in a new tab).

clean

Function signature
clean(path string) string

Returns the shortest path name equivalent to path by purely lexical processing.

Example
>>> filepath.clean("foo/bar/../baz")
"foo/baz"
>>> filepath.clean("foo/bar/./baz")
"foo/bar/baz"
>>> filepath.clean("foo/bar/../../baz")
"baz"

Learn more: filepath.Clean (opens in a new tab).

dir

Function signature
dir(path string) string

Returns all but the last element of path, typically the path's directory.

Example
>>> filepath.dir("foo/bar")
"foo"
>>> filepath.dir("foo/bar/")
"foo"
>>> filepath.dir("test.txt")
"."

Learn more: filepath.Dir (opens in a new tab).

ext

Function signature
ext(path string) string

Returns the file name extension used by path. The extension is the suffix beginning at the final dot in the final element of path. The result it is empty if there is no dot.

Example
>>> filepath.ext("foo/bar.txt")
".txt"
>>> filepath.ext("foo/bar")
""
>>> filepath.ext("foo/bar.tar.gz")
".gz"

Learn more: filepath.Ext (opens in a new tab).

is_abs

Function signature
is_abs(path string) bool

Returns true if the path is absolute.

Example
>>> filepath.is_abs("/foo/bar")
true
>>> filepath.is_abs("foo/bar")
false

Learn more: filepath.IsAbs (opens in a new tab).

join

Function signature
join(paths ...string) string

Returns the result of joining the given path elements with the operating system-specific path separator.

Example
>>> filepath.join("foo", "bar")
"foo/bar"
>>> filepath.join("foo", "bar", "baz")
"foo/bar/baz"

Learn more: filepath.Join (opens in a new tab).

match

Function signature
match(pattern, name string) bool

Returns true if the file name matches the shell pattern.

Example
>>> filepath.match("*.txt", "foo.txt")
true
>>> filepath.match("*.txt", "foo.tar.gz")
false

Learn more: filepath.Match (opens in a new tab).

rel

Function signature
rel(basepath, targpath string) string

Returns a relative path that is lexically equivalent to targpath when joined to basepath with an intervening separator.

Example
>>> filepath.rel("/home/user", "/home/user/foo/bar")
"foo/bar"
>>> filepath.rel("/home/user", "/home/user/foo/../bar")
"bar"
>>> filepath.rel("/home/user", "/home/user/foo/../../bar")
"../bar"

Learn more: filepath.Rel (opens in a new tab).

split_list

Function signature
split_list(path string) []string

Splits path immediately following the final separator, separating it into a directory and file name component. If there is no separator in path, split_list returns an empty dir and file set to path.

Example
>>> filepath.split_list("/home/user/foo/bar")
["/home/user/foo", "bar"]
>>> filepath.split_list("/home/user/foo")
["/home/user", "foo"]
>>> filepath.split_list("foo")
["", "foo"]

Learn more: filepath.Split (opens in a new tab).

split

Function signature
split(path string) []string

Splits the path immediately following the final separator, returning a list of two items: the directory and the file name. If there is no separator in the path, an empty directory and the file name are returned.

Example
>>> filepath.split("/home/user/foo/bar")
["/home/user/foo", "bar"]
>>> filepath.split("/home/user/foo")
["/home/user", "foo"]
>>> filepath.split("test.txt")
["", "test.txt"]

Learn more: filepath.Split (opens in a new tab).

walk_dir

Function signature
walk_dir(root string, fn func(path string))

Walks the file tree at root, calling fn for each file or directory in the tree, including root. Files are walked in lexical order. Symbolic links are not followed.

Example
>>> filepath.walk_dir("/home/user/foo", func(path, dir_entry, err) { print(path) })
"/home/user/foo"
"/home/user/foo/bar"
"/home/user/foo/test.txt"

Learn more: filepath.WalkDir (opens in a new tab).