Skip to content

Folder Operations

The rite.filesystem.folder submodule provides helpers for creating and inspecting directories and listing files.

Rite - Folder Module

This module provides utilities for folder management and manipulation within the Rite application.

Modules

folder_content_delete

Folder Content Delete Module

Deletes all files and subdirectories inside a given folder.

Functions

delete_contents(folder: str | Path, *, dry_run: bool = False, verbose: bool = True) -> None

Deletes all files and subdirectories inside a given folder. The folder itself is preserved.

Parameters:

Name Type Description Default
folder str | Path

The directory whose contents will be deleted.

required
dry_run bool

If True, shows what would be deleted without performing it.

False
verbose bool

If True, prints or logs deleted paths.

True

folder_create

Folder Create Module

This module provides functions to create directories.

Functions

create_directory(path: str | Path, mode: int = 511) -> Path

Ensures the given directory exists and returns the Path object.

Parameters:

Name Type Description Default
path str | Path

The directory path to create.

required
mode int

Permissions mode (default: 0o777).

511

Returns:

Name Type Description
Path Path

A pathlib.Path object representing the created or existing directory.

folder_ensure_exists

Folder Ensure Exists

Create a folder (and parents) if it does not already exist.

Functions

folder_ensure_exists(path: str | Path) -> None

Ensure that path exists as a directory, creating parents as needed.

folder_list_files

Folder List Files

Iterate over files contained in a directory.

Functions

folder_list_files(path: str | Path, recursive: bool = False) -> Iterator[Path]

Yield files in the given folder.

Args
path: Folder path.
recursive: If ``True``, walk recursively using ``Path.rglob``.
Yields
pathlib.Path: File paths found under *path*.

folder_list_folders

Folder List Folders Module

Lists all subdirectories inside a given directory.

Functions

list_folders(rootdir: str | Path, pattern: str | None = None, recursive: bool = False) -> list[Path]

Lists all subdirectories inside a given directory.

Parameters:

Name Type Description Default
rootdir str | Path

The directory to search.

required
pattern str | None

Optional glob pattern to filter directories (e.g., 'data*').

None
recursive bool

If True, searches recursively through all subfolders.

False

Returns:

Type Description
list[Path]

list[Path]: A list of Path objects representing found directories.

folder_size_get

Folder Size Get Module

Calculate the total size of all files in a folder recursively.

Functions

get_folder_size(folder_path: Path) -> int

Calculate the total size of all files in a folder recursively.

Parameters:

Name Type Description Default
folder_path Path

The path to the folder.

required

Returns:

Name Type Description
int int

Total size in bytes.

folder_size_to_string

Folder Size To String

Compute the total size of a folder and return a human-readable string.

Functions

folder_size_to_string(path: str | Path, recursive: bool = True) -> str

Return the total size of a folder as a human-readable string.

This walks the folder, summing file sizes using Path.stat and then formatting the aggregate size via :func:file_size_to_string.

Args
path: Folder path.
recursive: If ``True``, walk subfolders recursively.
Returns
str: Human-readable size such as ``"12.3 MB"``.

options: show_root_heading: true show_source: false heading_level: 2