def METHOD_NAME(self, filename: str = None):
    if self._file is not None:
        self._file.close()
    try:
        self._file = open(filename, mode='a', encoding='utf-8', errors=self.errors)
    except FileNotFoundError:  # directory does not exist
        path = Path(os.path.dirname(filename))
        try:
            path.mkdir(mode=0o755, parents=True, exist_ok=True)
        except OSError:
            self.logger.exception('Directory %r could not be created.', path)
            self.stop()
        else:
            self._file = open(filename, mode='a', encoding='utf-8', errors=self.errors)