dwrappr.filehandler module

dwrappr.filehandler.check_and_create_folder(path: str) None[source]

Checks if the directory for the given file path exists, and creates it if it does not.

This function ensures that the directory structure for a specified file path is available. If the directory does not exist, it will create all the necessary subdirectories. Logs information when a directory is successfully created.

Args:

path (str): The file path whose directory needs to be checked and potentially created.

Returns:

None

dwrappr.filehandler.check_file_exists(filepath) None[source]

Checks if a file exists at the specified file path.

This function verifies the existence of a file at the given file path. If the file does not exist, it raises a FileNotFoundError. It does not return any value and is used solely for validation purposes.

Args:

filepath (str): The path to the file to check for existence.

Raises:

FileNotFoundError: If the file does not exist at the specified file path.

dwrappr.filehandler.del_file_extension(file_path: str) str[source]

Removes the extension from a file path.

The function accepts a file path as input and removes its extension, returning the file path without the extension.

Args:

file_path (str): The path of the file whose extension is to be removed.

Returns:

str: The file path without its extension.

dwrappr.filehandler.get_file_extension(path: str) str[source]

Retrieve the file extension from the given file path.

This function extracts and returns the file extension from a given file path string. It uses the os.path.splitext method to split the path and obtain the extension.

Args:

path (str): The file path from which to extract the extension.

Returns:

str: The file extension, including the leading period.

dwrappr.filehandler.get_folder_files(directory: str, recursive: bool = True) List[str][source]

Retrieves a list of file paths from a specified directory.

This function returns the file paths in the provided directory as a list. The function can operate recursively to include files from subdirectories or non-recursively to include files from the top-level directory only.

Args:

directory (str): The directory path from which to retrieve the file paths.

recursive (bool, optional): If True, retrieves files from the directory and all subdirectories. Defaults to True.

Returns:

List[str]: A list containing file paths as strings.

dwrappr.filehandler.get_folder_name(filepath: str) str[source]

Extracts the folder name from the provided file path.

The function takes a file path as input and returns the directory portion of the path, which represents the folder containing the file.

Args:

filepath (str): The absolute or relative file path as a string.

Returns:

str: The folder or directory name extracted from the given file path.

Example:
>>> get_folder_name(filepath='/path/to/file.txt')
'/path/to'
>>> get_folder_name(filepath='folder/subfolder/data.csv')
'folder/subfolder'
dwrappr.filehandler.load_file(file_path: str) any[source]

Loads a file based on the provided path.

This function provides support for loading files based on the provided path. Supports .joblib, .json, and .yaml/.yml file types. Ensures relative file paths are resolved against the current working directory if no directory is specified in the given path. Raises ValueError if the file type is not supported.

Args:

file_path (str) : Path to the input file.

Returns:

The content of the loaded file. The type of the returned content depends on the file type and its structure.

Raises:

ValueError: Raised if the file type is not supported.

dwrappr.filehandler.save_file(data: any, path: str, autocreate_folder: bool = True) None[source]

Saves data to a file at the specified path.

This function supports saving in JSON, YAML/YML, and Joblib formats. If enabled, automatically creates the necessary folder structure for the file if it does not already exist. Logs an informational message upon successful save.

Args:

data (Any): The data to be saved to the file. path (str): The file path including the file name and extension where the data should be saved. autocreate_folder (bool, optional): If True, automatically creates the folder structure required by the specified path if it does not exist. Defaults to True.

Raises:

ValueError : If the file extension is not supported.

Returns:

None