Module pl.path
Path manipulation and file queries.
This is modelled after Python's os.path library (10.1); see the Guide.
NOTE: the functions assume the paths being dealt with to originate from the OS the application is running on. Windows drive letters are not to be used when running on a Unix system for example. The one exception is Windows paths to allow both forward and backward slashes (since Lua also accepts those)
Functions
dir () | Lua iterator over the entries of a given directory. |
mkdir () | Creates a directory. |
rmdir () | Removes a directory. |
attrib () | Gets attributes. |
currentdir () | Get the working directory. |
link_attrib () | Gets symlink attributes. |
chdir () | Changes the working directory. |
isdir (P) | is this a directory? |
isfile (P) | is this a file? |
getsize (P) | return size of a file. |
exists (P) | does a path exist? |
getatime (P) | Return the time of last access as the number of seconds since the epoch. |
getmtime (P) | Return the time of last modification as the number of seconds since the epoch. |
getctime (P) | Return the system's ctime as the number of seconds since the epoch. |
splitpath (P) | given a path, return the directory part and a file part. |
abspath (P[, pwd]) | return an absolute path. |
splitext (P) | given a path, return the root part and the extension part. |
dirname (P) | return the directory part of a path |
basename (P) | return the file part of a path |
extension (P) | get the extension part of a path. |
isabs (P) | is this an absolute path? |
join (p1, p2, ...) | return the path resulting from combining the individual paths. |
normcase (P) | normalize the case of a pathname. |
normpath (P) | normalize a path name. |
relpath (P[, start]) | relative path from current directory or optional start point |
expanduser (P) | Replace a starting '~' with the user's home directory. |
tmpname () | Return a suitable full path to a new temporary file name. |
common_prefix (path1, path2) | return the largest common prefix path of two paths. |
package_path (mod) | return the full path where a particular Lua module would be found. |
Fields
is_windows | are we running Windows? |
sep | path separator for this platform. |
dirsep | separator for PATH for this platform |
Functions
- dir ()
-
Lua iterator over the entries of a given directory.
Implicit link to
luafilesystem.dir
- mkdir ()
-
Creates a directory.
Implicit link to
luafilesystem.mkdir
- rmdir ()
-
Removes a directory.
Implicit link to
luafilesystem.rmdir
- attrib ()
-
Gets attributes.
Implicit link to
luafilesystem.attributes
- currentdir ()
-
Get the working directory.
Implicit link to
luafilesystem.currentdir
- link_attrib ()
-
Gets symlink attributes.
Implicit link to
luafilesystem.symlinkattributes
- chdir ()
-
Changes the working directory.
On Windows, if a drive is specified, it also changes the current drive. If
only specifying the drive, it will only switch drive, but not modify the path.
Implicit link to
luafilesystem.chdir
- isdir (P)
-
is this a directory?
Parameters:
- P string A file path
- isfile (P)
-
is this a file?
Parameters:
- P string A file path
- getsize (P)
-
return size of a file.
Parameters:
- P string A file path
- exists (P)
-
does a path exist?
Parameters:
- P string A file path
Returns:
-
the file path if it exists (either as file, directory, socket, etc), nil otherwise
- getatime (P)
-
Return the time of last access as the number of seconds since the epoch.
Parameters:
- P string A file path
- getmtime (P)
-
Return the time of last modification as the number of seconds since the epoch.
Parameters:
- P string A file path
- getctime (P)
-
Return the system's ctime as the number of seconds since the epoch.
Parameters:
- P string A file path
- splitpath (P)
-
given a path, return the directory part and a file part.
if there's no directory part, the first value will be empty
Parameters:
- P string A file path
Returns:
- directory part
- file part
Usage:
local dir, file = path.splitpath("some/dir/myfile.txt") assert(dir == "some/dir") assert(file == "myfile.txt") local dir, file = path.splitpath("some/dir/") assert(dir == "some/dir") assert(file == "") local dir, file = path.splitpath("some_dir") assert(dir == "") assert(file == "some_dir")
- abspath (P[, pwd])
-
return an absolute path.
Parameters:
- splitext (P)
-
given a path, return the root part and the extension part.
if there's no extension part, the second value will be empty
Parameters:
- P string A file path
Returns:
- string root part (everything upto the "."", maybe empty)
- string extension part (including the ".", maybe empty)
Usage:
local file_path, ext = path.splitext("/bonzo/dog_stuff/cat.txt") assert(file_path == "/bonzo/dog_stuff/cat") assert(ext == ".txt") local file_path, ext = path.splitext("") assert(file_path == "") assert(ext == "")
- dirname (P)
-
return the directory part of a path
Parameters:
- P string A file path
Returns:
-
string
everything before the last dir-separator
See also:
Usage:
path.dirname("/some/path/file.txt") -- "/some/path" path.dirname("file.txt") -- "" (empty string)
- basename (P)
-
return the file part of a path
Parameters:
- P string A file path
Returns:
See also:
Usage:
path.basename("/some/path/file.txt") -- "file.txt" path.basename("/some/path/file/") -- "" (empty string)
- extension (P)
-
get the extension part of a path.
Parameters:
- P string A file path
Returns:
See also:
Usage:
path.extension("/some/path/file.txt") -- ".txt" path.extension("/some/path/file_txt") -- "" (empty string)
- isabs (P)
-
is this an absolute path?
Parameters:
- P string A file path
Usage:
path.isabs("hello/path") -- false path.isabs("/hello/path") -- true -- Windows; path.isabs("hello\path") -- false path.isabs("\hello\path") -- true path.isabs("C:\hello\path") -- true path.isabs("C:hello\path") -- false
- join (p1, p2, ...)
-
return the path resulting from combining the individual paths.
if the second (or later) path is absolute, we return the last absolute path (joined with any non-absolute paths following).
empty elements (except the last) will be ignored.
Parameters:
Returns:
-
string
the combined path
Usage:
path.join("/first","second","third") -- "/first/second/third" path.join("first","second/third") -- "first/second/third" path.join("/first","/second","third") -- "/second/third"
- normcase (P)
-
normalize the case of a pathname. On Unix, this returns the path unchanged, for Windows it converts;
- the path to lowercase
- forward slashes to backward slashes
Parameters:
- P string A file path
Usage:
path.normcase("/Some/Path/File.txt") -- Windows: "\some\path\file.txt" -- Others : "/Some/Path/File.txt"
- normpath (P)
-
normalize a path name.
A//B
,A/./B
, andA/foo/../B
all becomeA/B
.An empty path results in '.'.
Parameters:
- P string a file path
- relpath (P[, start])
-
relative path from current directory or optional start point
Parameters:
- expanduser (P)
-
Replace a starting '~' with the user's home directory.
In windows, if HOME isn't set, then USERPROFILE is used in preference to
HOMEDRIVE HOMEPATH. This is guaranteed to be writeable on all versions of Windows.
Parameters:
- P string A file path
Returns:
-
string
The file path with the
~
prefix substituted, or the input path if it had no prefix.Or
- nil
- string Error message if the environment variables were unavailable.
- tmpname ()
- Return a suitable full path to a new temporary file name. unlike os.tmpname(), it always gives you a writeable path (uses TEMP environment variable on Windows)
- common_prefix (path1, path2)
-
return the largest common prefix path of two paths.
Parameters:
Returns:
-
the common prefix (Windows: separators will be normalized, casing will be original)
- package_path (mod)
-
return the full path where a particular Lua module would be found.
Both package.path and package.cpath is searched, so the result may
either be a Lua file or a shared library.
Parameters:
- mod string name of the module
Returns:
- on success: path of module, lua or binary
- on error: nil, error string listing paths tried