Skip to main content

Remove the file extension from a path (file.path.remove_extension)

Declaration​

new_path, err = file.path.remove_extension(path)

Parameters​

  • path
    String. The original path string.

Returns​

  • new_path
    String or nil. Returns the path string with its extension removed on success, or nil if the path is invalid.
  • err
    String. Returns "Invalid path" if the path is invalid.

Description​

Remove the extension and its preceding . from the last path component. If there are multiple extension segments, remove only the last one; if there is no extension, return the original path.
This function only processes the path string; it does not rename any actual file or directory.
This function is available only in versions released after 20250914.

Example​

local base = file.path.remove_extension("/path/to/file.txt") -- Returns "/path/to/file"
local archive_base = file.path.remove_extension("/path/to/archive.tar.gz") -- Returns "/path/to/archive.tar"
local unchanged = file.path.remove_extension("/path/to/file") -- Returns "/path/to/file"