pathtools - Filesystem fun

Functions for working with filesystem paths and finding files.

The expandpath() does recursive shell-like expansion of paths from lists.

torxtools.pathtools.cachedir(appname: str, path: str) str

Find a suitable location for cache files.

Parameters:
  • appname (str) – Name of application. Used as last part of the cachedir path.

  • path (str) – a single path, a list of paths, or none.

Returns:

a suitable cachedir, created if not existing

Return type:

str, None

torxtools.pathtools.expandpath(path: str | List[str] | None) str | List[str] | None

Recursive shell-like expansion of environment variables and tilde home directory.

Parameters:

path (str, [str], None) – a single path, a list of paths, or none.

Returns:

a single expanded path, a list of expanded path, or none

Return type:

str, [str], None

Example

import os
from torxtools import pathtools

os.environ["SPAM"] = "eggs"
assert pathtools.expandpath(["~/$SPAM/one", "~/$SPAM/two"]) == [
    os.path.expanduser("~/eggs/one"),
    os.path.expanduser("~/eggs/two"),
]

See also

boltons:boltons.pathutils.expandpath()

torxtools.pathtools.find_pyproject(path: str) str

Find location of “pyproject.toml”

Parameters:

path (str) – a single path to a directory to search down recursively.

Returns:

a absolute path to the location of ‘pyproject.toml’

Return type:

str

Example

import os
from torxtools import pathtools

pyproject = pathtools.find_pyproject(os.path.dirname(__file__))