Module pl.dir
Useful functions for getting directory contents and matching them against wildcards.
Dependencies: pl.utils , pl.path , pl.tablex
Soft Dependencies: alien
, ffi
(either are used on Windows for copying/moving files)
Functions
fnmatch (file, pattern) | does the filename match the shell pattern?. |
filter (files, pattern) | return a list of all files which match the pattern. |
getfiles (dir, mask) | return a list of all files in a directory which match the a shell pattern. |
getdirectories (dir) | return a list of all subdirectories of the directory. |
copyfile (src, dest, flag) | copy a file. |
movefile (src, dest) | move a file. |
walk (root, bottom_up, follow_links) | return an iterator which walks through a directory tree starting at root. |
rmtree (fullpath) | remove a whole directory tree. |
makepath (p) | create a directory path. |
clonetree (path1, path2, file_fun, verbose) | clone a directory tree. |
dirtree (d) | return an iterator over all entries in a directory tree |
getallfiles (start_path, pattern) | Recursively returns all the file starting at path. |
Functions
- fnmatch (file, pattern)
-
does the filename match the shell pattern?.
(cf. fnmatch.fnmatch in Python, 11.8)
Parameters:
file
: A file namepattern
: A shell pattern
Returns:
-
true or false
Raises:
file and pattern must be strings - filter (files, pattern)
-
return a list of all files which match the pattern.
(cf. fnmatch.filter in Python, 11.8)
Parameters:
files
: A table containing file namespattern
: A shell pattern.
Returns:
-
list of files
Raises:
file and pattern must be strings - getfiles (dir, mask)
-
return a list of all files in a directory which match the a shell pattern.
Parameters:
dir
: A directory. If not given, all files in current directory are returned.mask
: A shell pattern. If not given, all files are returned.
Returns:
-
lsit of files
Raises:
dir and mask must be strings - getdirectories (dir)
-
return a list of all subdirectories of the directory.
Parameters:
dir
: A directory
Returns:
-
a list of directories
Raises:
dir must be a string - copyfile (src, dest, flag)
-
copy a file.
Parameters:
src
: source filedest
: destination file or directoryflag
: true if you want to force the copy (default)
Returns:
-
true if operation succeeded
Raises:
src and dest must be strings - movefile (src, dest)
-
move a file.
Parameters:
src
: source filedest
: destination file or directory
Returns:
-
true if operation succeeded
Raises:
src and dest must be strings - walk (root, bottom_up, follow_links)
-
return an iterator which walks through a directory tree starting at root.
The iterator returns (root,dirs,files)
Note that dirs and files are lists of names (i.e. you must say path.join(root,d)
to get the actual full path)
If bottom_up is false (or not present), then the entries at the current level are returned
before we go deeper. This means that you can modify the returned list of directories before
continuing.
This is a clone of os.walk from the Python libraries.
Parameters:
root
: A starting directorybottom_up
: False if we start listing entries immediately.follow_links
: follow symbolic links
Returns:
-
an iterator returning root,dirs,files
Raises:
root must be a string - rmtree (fullpath)
-
remove a whole directory tree.
Parameters:
fullpath
: A directory path
Returns:
- true or nil
- error if failed
Raises:
fullpath must be a string - makepath (p)
-
create a directory path.
This will create subdirectories as necessary!
Parameters:
p
: A directory path
Returns:
-
a valid created path
Raises:
p must be a string - clonetree (path1, path2, file_fun, verbose)
-
clone a directory tree. Will always try to create a new directory structure
if necessary.
Parameters:
path1
: the base path of the source treepath2
: the new base path for the destinationfile_fun
: an optional function to apply on all filesverbose
: an optional boolean to control the verbosity of the output. It can also be a logging function that behaves like print()
Usage:
clonetree('.','../backup',copyfile)
Returns:
- true, or nil
- error message, or list of failed directory creations
- list of failed file operations
Raises:
path1 and path2 must be strings - dirtree (d)
-
return an iterator over all entries in a directory tree
Parameters:
d
: a directory
Returns:
-
an iterator giving pathname and mode (true for dir, false otherwise)
Raises:
d must be a non-empty string - getallfiles (start_path, pattern)
-
Recursively returns all the file starting at path. It can optionally take a shell pattern and
only returns files that match <i>pattern</i>. If a pattern is given it will do a case insensitive search.
Parameters:
start_path
: {string} A directory. If not given, all files in current directory are returned.pattern
: {string} A shell pattern. If not given, all files are returned.
Returns:
-
Table containing all the files found recursively starting at path and filtered by pattern.
Raises:
start_path must be a string