Skip to content

FileRemoverMixin

A helper class to delete files from the system.

Source code in tools/mixins/file_remover.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
class FileRemoverMixin:
    """A helper class to delete files from the system."""
    def remove_all(self, filepaths: Union[List[Path], Tuple[Path], Path]) -> None:
        """Deletes all the files in the given iterable or path.

        Args:
            filepaths (Union[List[Path], Tuple[Path], Path]): A list of paths,
                a tuple of paths, or a single path to delete.

        Raises:
            TypeError: If the input is not a list, tuple, or Path.
        """
        if isinstance(filepaths, (list, tuple)):
            for path in filepaths:
                self.remove_file(path)

        elif isinstance(filepaths, Path):
            self.remove_file(filepaths)

        else:
            raise TypeError(f'filepaths should be a list or a tuple or a Path, not {type(filepaths)}')


    def remove_file(self: LoggerProtocol, path: Path) -> bool:
        """Deletes one file from the system.

        Args:
            path (Path): The path of the file to delete.

        Returns:
            bool: True if the file was deleted successfully, False otherwise.
        """
        if not path.is_file():
            self.logger.warning(f"{path} is not a file")
        try:
            path.unlink(missing_ok=True)
            self.logger.info(f"{path} removed")
            return True
        except FileNotFoundError:
            self.logger.warning(f"{path} file not exists, skipping")
            return False

remove_all(filepaths)

Deletes all the files in the given iterable or path.

Parameters:

Name Type Description Default
filepaths Union[List[Path], Tuple[Path], Path]

A list of paths, a tuple of paths, or a single path to delete.

required

Raises:

Type Description
TypeError

If the input is not a list, tuple, or Path.

Source code in tools/mixins/file_remover.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
def remove_all(self, filepaths: Union[List[Path], Tuple[Path], Path]) -> None:
    """Deletes all the files in the given iterable or path.

    Args:
        filepaths (Union[List[Path], Tuple[Path], Path]): A list of paths,
            a tuple of paths, or a single path to delete.

    Raises:
        TypeError: If the input is not a list, tuple, or Path.
    """
    if isinstance(filepaths, (list, tuple)):
        for path in filepaths:
            self.remove_file(path)

    elif isinstance(filepaths, Path):
        self.remove_file(filepaths)

    else:
        raise TypeError(f'filepaths should be a list or a tuple or a Path, not {type(filepaths)}')

remove_file(path)

Deletes one file from the system.

Parameters:

Name Type Description Default
path Path

The path of the file to delete.

required

Returns:

Name Type Description
bool bool

True if the file was deleted successfully, False otherwise.

Source code in tools/mixins/file_remover.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
def remove_file(self: LoggerProtocol, path: Path) -> bool:
    """Deletes one file from the system.

    Args:
        path (Path): The path of the file to delete.

    Returns:
        bool: True if the file was deleted successfully, False otherwise.
    """
    if not path.is_file():
        self.logger.warning(f"{path} is not a file")
    try:
        path.unlink(missing_ok=True)
        self.logger.info(f"{path} removed")
        return True
    except FileNotFoundError:
        self.logger.warning(f"{path} file not exists, skipping")
        return False