Get the file extension of a path (file.path.extension)
Declaration
extension, err = file.path.extension(path)
Parameters
- path
String. The path string.
Returns
- extension
String or nil. Returns the extension without the leading., an empty string""if there is no extension, or nil if the path is invalid. - err
String. Returns"Invalid path"if the path is invalid.
Description
Return the file extension of the last path component. If there are multiple extension segments, return only the last one; for example,
archive.tar.gzreturnsgz.
This function is available only in versions released after 20250914.
Example
local ext = file.path.extension("/path/to/file.txt") -- Returns "txt"
local archive_ext = file.path.extension("/path/to/archive.tar.gz") -- Returns "gz"
local no_ext = file.path.extension("/path/to/file") -- Returns ""