path#
local path = require 'path'
The path module contains functions for analyzing and altering file
path strings.
It is essentially an interface to Rust’s std::path.
Functions#
- path.ancestors(path)#
Return a list of
pathand its ancestors.- Return type:
Example#local path = require 'path' -- a is: -- '../some/path/foo/bar' -- '../some/path/foo' -- '../some/path' -- '../some' -- '..' -- '' for i,a in ipairs(path.ancestors('../some/path/foo/bar')) do end
See also
Rust’s std::path::Path.ancestors.
Version History
Version
Notes
0.3.0
Added
- path.canonicalize(path)#
Return the canonical, absolute form of
pathwith all intermediate components and symbolic links resolved.If
pathis invalid,nilis returned instead.- Return type:
See also
Rust’s std::path::Path.canonicalize.
Version History
Version
Notes
0.3.0
Added
- path.components(path)#
Return a list of components that make up
path.When parsing the path, there is a small amount of normalization:
Repeated separators are ignored, so
a/banda//bboth haveaandbas components.Occurrences of
.are normalized away, except if they are at the beginning of the path. For example,a/./b,a/b/,a/b/. anda/ball haveaandbas components, but./a/bstarts with an additionalcur-dircomponent.A trailing slash is normalized away,
/a/band/a/b/are equivalent.Note that no other normalization takes place; in particular,
a/canda/b/../care distinct, to account for the possibility thatbis a symbolic link (so its parent isn’ta).Components are returned as a sequence of strings. The first string is the type of component, and any following strings are the path fragment(s) of the component.
The component type will be one of the following values:
Value
Description
prefix-verbatim
A verbatim prefix, e.g.
\\?\\foo.prefix-verbatim-unc
A verbatim prefix using Windows’ Uniform Naming Convention, e.g.
\\?\\UNC\server\share.prefix-verbatim-disk
A verbatim disk prefix, e.g.
\\?\C:.prefix-device-ns
A device namespace prefix, e.g.
\\.\COM1.prefix-unc
A Windows’ Uniform Naming Convention prefix, e.g.
\\server\share.prefix-disk
A prefix for a given disk drive, e.g.
C:.root-dir
The root directory component, appears after any prefix an before anything else.
cur-dir
A reference to the current directory, i.e.
..parent-dir
A reference to the parent directory, i.e.
...normal
A normal path component, e.g.
aandbina/b.- Return type:
Example#local path = require 'path' -- c is: -- {'parent-dir'} -- {'normal', 'foo'} -- {'normal', 'bar'} for i,c in ipairs(path.components('../foo/bar')) do end
See also
Rust’s std::path::Path.components.
Version History
Version
Notes
0.3.0
Added
- path.endswith(path, child)#
Returns
trueifchildis a suffix ofpath.Only considers whole path components to match.
- Return type:
See also
Rust’s std::path::Path.ends_with.
Version History
Version
Notes
0.3.0
Added
- path.exists(path)#
Returns
trueifpathpoints at an existing entity.If an error occurs, information is logged and
nilis returned instead.- Return type:
See also
Rust’s std::path::Path.try_exists.
Version History
Version
Notes
0.3.0
Added
- path.extension(path)#
Return the extension (without the leadin dot) of
path, if possible.The extension is:
nilif there is no file namenilif there is no embedded.nilif the file name begins with.and no other.withinOtherwise, the portion of the file name after the final
.
- Return type:
See also
Rust’s std::path::Path.extension.
Version History
Version
Notes
0.3.0
Added
- path.filename(path)#
Return the final component of
path, if there is one.If the path is a normal file, this is the file name. If it’s the path of a directory, this is the directory name.
Returns
nilif the path terminates in...- Return type:
See also
Rust’s std::path::Path.file_name.
Version History
Version
Notes
0.3.0
Added
- path.filestem(path)#
Return the stem (non-extension) portion of the file name in
path.The stem is:
nilif there is no file nameThe entire file name if there is no embedded
.The entire file name if the file name begins with
.and has no other.withinOtherwise, the portion of the file name before the final
.
- Return type:
See also
Rust’s std::path::Path.file_stem.
Version History
Version
Notes
0.3.0
Added
- path.hasroot(path)#
Return
trueifpathhas a root component.- Return type:
See also
Rust’s std::path::Path.has_root.
Version History
Version
Notes
0.3.0
Added
- path.isabsolute(path)#
Return
trueifpathis absolute, i.e., it is independent of the current directory.- Return type:
See also
Rust’s std::path::Path.is_absolute.
Version History
Version
Notes
0.3.0
Added
- path.isdir(path)#
Return
trueifpathexists and is pointing at a directory.- Return type:
See also
Rust’s std::path::Path.is_dir.
Version History
Version
Notes
0.3.0
Added
- path.isfile(path)#
Return
trueifpathexists and is pointing at a regular file.- Return type:
See also
Rust’s std::path::Path.is_file.
Version History
Version
Notes
0.3.0
Added
- path.isrelative(path)#
Return
trueifpathis relative.- Rtype::
boolean
See also
Rust’s std::path::Path.is_relative.
Version History
Version
Notes
0.3.0
Added
- path.issymlink(path)#
Return
trueifpathexists and is pointing at a symbolic link.- Return type:
See also
Rust’s std::path::Path.is_symlink.
Version History
Version
Notes
0.3.0
Added
- path.join(path1, path2)#
Return a new path from
path2adjoined topath1.If
path2is absolute, it will be returned instead.- Return type:
See also
Rust’s std::path::Path.join.
Version History
Version
Notes
0.3.0
Added
- path.parent(path)#
Return
pathwithout its final component, if there is one.Returns
''for relative paths with one component, ornilif the path terminates in a root or prefix, or if it’s an empty string.- Return type:
See also
Rust’s std::path::Path.parent.
Version History
Version
Notes
0.3.0
Added
- path.startswith(path, base)#
Returns
trueifbaseif a prefix ofpath.Only considers whole path components to match.
- Return type:
See also
Rust’s std::path::Path.starts_with.
Version History
Version
Notes
0.3.0
Added
- path.stripprefix(path, base)#
Returns a path that when joined onto base yields
path.- Return type:
See also
Rust’s std::path::Path.strip_prefix.
Version History
Version
Notes
0.3.0
Added
- path.withextension(path, extension)#
Return
pathbut with the givenextension.- Return type:
See also
Rust’s std::path::Path.with_extension.
Version History
Version
Notes
0.3.0
Added
- path.withfilename(path, filename)#
Return
pathbut with the givenfilename.If
pathalready ends in a filename, it will be replaced.- Return type:
See also
Rust’s std::path::Path.with_file_name.
Version History
Version
Notes
0.3.0
Added