filepath
The filepath module contains utilities thare are used to manipulate file paths
with operating system-specific separators.
Functions
abs
abs(path string) stringReturns 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.
>>> 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
base(path string) stringReturns the last element of path with any trailing slashes removed. If path is empty, "." is returned.
>>> 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
clean(path string) stringReturns the shortest path name equivalent to path by purely lexical processing.
>>> 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
dir(path string) stringReturns all but the last element of path, typically the path's directory.
>>> filepath.dir("foo/bar")
"foo"
>>> filepath.dir("foo/bar/")
"foo"
>>> filepath.dir("test.txt")
"."Learn more: filepath.Dir (opens in a new tab).
ext
ext(path string) stringReturns 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.
>>> 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
is_abs(path string) boolReturns true if the path is absolute.
>>> filepath.is_abs("/foo/bar")
true
>>> filepath.is_abs("foo/bar")
falseLearn more: filepath.IsAbs (opens in a new tab).
join
join(paths ...string) stringReturns the result of joining the given path elements with the operating system-specific path separator.
>>> filepath.join("foo", "bar")
"foo/bar"
>>> filepath.join("foo", "bar", "baz")
"foo/bar/baz"Learn more: filepath.Join (opens in a new tab).
match
match(pattern, name string) boolReturns true if the file name matches the shell pattern.
>>> filepath.match("*.txt", "foo.txt")
true
>>> filepath.match("*.txt", "foo.tar.gz")
falseLearn more: filepath.Match (opens in a new tab).
rel
rel(basepath, targpath string) stringReturns a relative path that is lexically equivalent to targpath when joined to basepath with an intervening separator.
>>> 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
split_list(path string) []stringSplits 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.
>>> 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
split(path string) []stringSplits 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.
>>> 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
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.
>>> 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).