lair.records.list_files#

lair.records.list_files(path='.', pattern=None, ignore_case=False, all_files=False, full_names=False, recursive=False, followlinks=False)[source]#

Return a list of files in the specified directory that match the specified pattern.

Parameters:
  • path (str, optional) – The directory to search for files. Defaults to the current directory.

  • pattern (str, optional) – The glob-style pattern to match against file names. Defaults to None, which matches all files.

  • ignore_case (bool, optional) – Whether to ignore case when matching file names. Defaults to False.

  • all_files (bool, optional) – Whether to include hidden files (files that start with a dot). Defaults to False.

  • full_names (bool, optional) – Whether to return the full path of each file. Defaults to False.

  • recursive (bool, optional) – Whether to search for files recursively in subdirectories. Defaults to False.

  • followlinks (bool, optional) – Whether to follow symbolic links. Defaults to False.

Returns:

List[str] – A list of file names or full paths that match the specified pattern.

Return type:

list[str]

See also

glob.glob

Matches a pattern in a path, which can hold wildcards.

Examples

>>> import tempfile
>>> from pathlib import Path
>>> from lair.records import list_files
>>> folder = Path(tempfile.mkdtemp())
>>> for name in ["a.nc", "b.nc", "Notes.txt", ".hidden.nc"]:
...     (folder / name).touch()
>>> sorted(list_files(folder, "*.nc"))
['a.nc', 'b.nc']
>>> sorted(list_files(folder, "*.TXT", ignore_case=True))
['Notes.txt']
>>> sorted(list_files(folder, "*.nc", all_files=True))
['.hidden.nc', 'a.nc', 'b.nc']