Skip to content

Repository files navigation

lpath - Path utils for Lua

CICoverage Status

lpath is a lfs-like Lua module to handle path, file system and file informations.

This module is inspired by Python's os.path and pathlib module. It split into 4 parts:

  • path: main module, pathlib style path operations.
  • path.fs: fs specific operations, folder walking, file operations, etc.
  • path.info: some constants about path literals.
  • path.env set/get environment variables, and expand env vars in path.

All routines in this module which accept ... for parameters means you could pass any count of string as arguments. All string will joined into a single path, just as pass all arguments to path(...), and pass the resulting path string to the routine.

All routines may returns nil, error for error case. if you want raise error to Lua, use assert(...) for routines.

Usage

path

routinereturn valuedescription
path(...)stringreturn joined normalized path string.
path.ansi()noneset path string encoding to local code page.
path.ansi(number)noneset the code page number for path string encoding.
path.ansi(string)stringconvert UTF-8 string to current code page encoding.
path.utf8()noneset path string encoding to UTF-8.
path.utf8(string)stringconvert current code page encoding string to UTF-8.
path.alt(...)stringreturn joined normalized path string using alternative sep.
path.abs(...)stringreturns the absolute path for joined parts.
path.rel(path[, dir])stringreturns the relation path for dir (default for current work directory).
path.fnmatch(string, pattern)booleanreturns whether the pattern matchs the string.
path.match(path, pattern)booleanreturns as path.fnmatch, but using Python path matching rules.
path.drive(...)stringreturns the drive part of path.
path.root(...)stringreturns the root part of path. (\ on Windows, / or // on POSIX systems.)
path.anchor(...)stringsame as path.drive(...) .. path.root(...)
path.parent(...)stringreturns the parent path for path.
path.name(...)stringreturns the file name part of the path.
path.stem(...)stringreturns the file name part without suffix name of the path.
path.suffix(...)stringreturns the suffix name of the path.
path.suffixes(...)iteraotrreturns a idx, suffix iterator to get suffix names of the path.
path.parts(...)iteratorreturns a idx, part iterator to get parts in the path.
path.exists(...)booleanreturns whether the path is exists in file system (same as fs.exists())
path.resolve(...)stringreturns the path itself, or the target path if path is a symlink.
path.cwd()stringfetch the current working directory path.
path.bin()stringfetch the current executable file path.
path.isdir(...)booleanreturns whether the path is a directory.
path.islink(...)booleanreturns whether the path is a symlink.
path.isfile(...)booleanreturns whether the path is a regular file.
path.ismount(...)booleanreturns whether the path is a mount point.

path.fs

routinereturn valuedescription
fs.dir(...)iteratorreturns a iterator filename, type to list all child items in path.
fs.scandir(...[, depth])iteratorsame as fs.dir, but walk into sub directories recursively.
fs.glob(...[, depth])iteratorsame as fs.scandir, but accepts a pattern for filter the items in directory.
fs.chdir(...)stringchange current working directory and returns the path, or nil for error.
fs.mkdir(...)stringcreate directory.
fs.rmdir(...)stringremove empty directory.
fs.makedirs(...)stringcreate directory recursively.
fs.remvoedirs(...)stringremove all items in a directory recursively.
fs.unlockdirs(...)stringadd write perimission for all files in a directory recursively.
fs.tmpdir(prefix)stringcreate a tmpdir and returns it's path
fs.ctime(...)integerreturns the creation time for the path.
fs.mtime(...)integerreturns the modify time for the path.
fs.atime(...)integerreturns the access time for the path.
fs.size(...)integerreturns the file size for the path.
fs.touch(...[, atime[, mtime]])stringupdate the access/modify time for the path file, if file is not exists, create it.
fs.remove(...)stringdelete file.
fs.copy(source, target)booleancopy file from the source path to the target path.
fs.rename(source, target)booleanmove file from the source path to the target path.
fs.symlink(source, target[, isdir])booleancreate a symbolic link from the source path to the target path.
fs.exists(...)booleansame as path.exists
fs.getcwd()stringsame as path.cwd()
fs.binpath()stringsame as path.bin()
fs.is{dir/link/file/mount}stringsame as correspond routines in path module.

fs.dir()/fs.scandir()/fs.glob()

These functions will return a iterator that yields filename, type pair. The type could be:

  • "file" a file name
  • "dir" a dir the will not walk into it.
  • "in" a dir that will walk into it, i.e. the next iteration will yields the content in this folder.
  • "out" a dir that completed walk.

If you pass a number argument as the last argument of fs.scandir()/fs.glob(), this number argument will be treat as the limit of walking. e.g. fs.scandir("foo", 1) will walks into all subdirectory/files in "foo", but not contents in subdirectories.

-- assume folder "foo" has this struture:-- - foo-- |- bar-- |- bar.txt-- |- foo.txt-- the code below:forfn, tyinfs.scandir("foo", 1) doprint(fn, ty)
end-- will prints:-- foo in-- bar dir-- foo.txt file-- foo out

fs.glob() accepts a path thats contains patterns in it. But patterns in drive part will be ignored. e.g. the pattern likes "*:/foo.txt" in Windows will yields empty results.

A empty pattern ("") is not allowed.

If a pattern contains "**", the fs.glob() will walks into all current subdirectories to find a match after "**", e.g. "**/*.txt" will yields all .txt files in any levels of subdirectories of current folder.

If the pattern ends with "**", all subdirectories, but not files, will returnd.

Some examples:

-- assume same struture of folder "foo" above.localfunctioncollect(pattern) dolocalt= {}
forfninfs.glob(pattern) dot[#t+1] =fnendendcollect"*.txt" -- returns {"foo/foo.txt"}collect"**/*.txt" -- returns {"foo/foo.txt", "foo/bar/bar.txt"}collect"**" -- returns {"foo/bar"}

path.env

routinereturn valuedescription
env.get(key)stringfetch a environment variable value.
env.set(key, value)stringset the environment variable value and returns the new value.
env.expand(...)stringreturn a path that all environment variables replaced.
env.uname()string, ...returns the informations for the current operation system.

path.info

path.info has several constants about current system:

  • platform:
    • "windows"
    • "linux"
    • "macosx"
    • "android"
    • "posix"
  • sep: separator of directory on current system. It's "\\" on Windows, "/" otherwise.
  • altsep: the alternative directory separator, always "/".
  • curdir: the current directory, usually ".".
  • pardir: the parent directory, usually "..".
  • devnull: the null device file, "nul" on Windows, "dev/null" otherwise
  • extsep: extension separator, usually ".".
  • pathsep: the separator for $PATH, ";" on Windows, otherwise ":".

License

Same as Lua's License.

Build

See here: http://lua-users.org/wiki/BuildingModules

About

a OS specified path manipulation module for Lua

Resources

Stars

44 stars

Watchers

4 watching

Forks

Releases

Packages

Used by

Contributors

Languages