File search and file system traversal

pshell.glob(pathname: str, *, min_results: int = '0', max_results: int | None = 'None')list
pshell.glob(pathname: pathlib.Path, *, min_results: int = '0', max_results: int | None = 'None')list

Like glob.glob(), but in addition it supports environment variables in pathname, logs the number of results, and incorporates protection from non-existing paths.

Parameters
  • pathname – Bash-like wildcard expression. Can be a string or a pathlib.Path.

  • min_results (int) – Minimum number of expected results

  • max_results (int) – Maximum number of expected results. Omit for no maximum.

Raises

FileMatchError – If found less results than min_results or more than max_results

Returns

List of matching files or directories. The return type of the outputs matches the type of pathname.

pshell.iglob(pathname: str, *, min_results: int = '0', max_results: int | None = 'None')collections.abc.Iterator[str]
pshell.iglob(pathname: pathlib.Path, *, min_results: int = '0', max_results: int | None = 'None')collections.abc.Iterator[pathlib.Path]

Like glob(), but returns an iterator instead. Notice that, unlike with glob, you may have time to process some of the results before FileMatchError is raised.

In case max_results is exceeded, the iteration will stop immediately - which will save time and memory.

Example:

>>> for fname in glob("test*.txt", max_results=2):
>>>    print(fname)
FileMatchError: File match test*.txt produced 4 results, expected up
                to 2

>>> for fname in iglob("test*.txt", max_results=2):
>>>    print(fname)
test1.txt
test2.txt
FileMatchError: File match test*.txt produced 3 or more results,
                expected up to 2
exception pshell.FileMatchError

glob() or iglob() returned not enough or too many matches