nicetoolbox.utils.filehandling

Helper functions for reading, writing and parsing files

Functions

find_json_files

Recursively find all json files in the given directory.

find_npz_files

Recursively find all npz files in the given directory.

load_json_file

Load JSON data from a file.

load_toml

Load TOML data from a file.

read_npz_file

Reads and returns the data from an NPZ file.

resolve_single_file

Resolve path to exactly one existing file.

nicetoolbox.utils.filehandling.find_json_files(directory)[source]

Recursively find all json files in the given directory.

Parameters:

directory (str) – The directory to search for json files.

Returns:

A list of paths to the json files found in the directory.

Return type:

json_files (list)

nicetoolbox.utils.filehandling.find_npz_files(directory)[source]

Recursively find all npz files in the given directory.

Parameters:

directory (str) – The directory to search for npz files.

Returns:

A list of paths to the npz files found in the directory.

Return type:

npz_files (list)

nicetoolbox.utils.filehandling.load_json_file(json_path: str) → dict[source]

Load JSON data from a file.

Parameters:

json_path (str) – The path to the JSON file.

Returns:

The JSON data loaded from the file.

Return type:

dict

Raises:
  • FileNotFoundError – If the file does not exist.

  • json.JSONDecodeError – If the file contains invalid JSON data.

nicetoolbox.utils.filehandling.load_toml(toml_path: str) → dict[source]

Load TOML data from a file.

Parameters:

toml_path (str) – The path to the TOML file.

Returns:

The TOML data loaded from the file.

Return type:

dict

nicetoolbox.utils.filehandling.read_npz_file(filepath)[source]

Reads and returns the data from an NPZ file.

Supports loading data with the allow_pickle=True parameter.

Parameters:

filepath (str) – The path to the NPZ file.

Returns:

The data loaded from the NPZ file.

Return type:

data (numpy.ndarray)

nicetoolbox.utils.filehandling.resolve_single_file(path: Path, *, label: str) → Path[source]

Resolve path to exactly one existing file.

  • If path contains a *, expand it as a glob; expect exactly one match.

  • Otherwise, expect the file to exist as-is.

Zero matches or multiple matches raise ValueError. label is included in error messages to identify the track/source that produced the path.