Penlight Documentation

# 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)

Dependencies: pl.utils, lfs

## 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), false 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:

  1. directory part
  2. 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:

  - P string A file path
  - pwd string optional start path to use (default is current dir) (optional)
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:

  1. string root part (everything upto the "."", maybe empty)
  2. 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:

  splitpath

  ### 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:

  string

  ### See also:

  splitpath

  ### 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:

  string

  ### See also:

  splitext

  ### 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:

  - p1 string A file path
  - p2 string A file path
  - ... string more file paths

  ### 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`, and `A/foo/../B` all become `A/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:

  - P string a path
  - start string optional start point (default current directory) (optional)
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

  1. nil
  2. 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:

  - path1 string a file path
  - path2 string a file path

  ### 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:

  1. on success: path of module, lua or binary
  2. on error: nil, error string listing paths tried

## Fields

is_windows
  are we running Windows?
sep
  path separator for this platform.
dirsep
  separator for PATH for this platform

generated by LDoc 1.5.0