def _switch_h5_file(self, filename, mode):
        """Switch to a different H5 file with the specified mode.

        This is useful for retrieving information from all shards, such as the
        total length, keys, and items.
        """
        if mode not in ("r", "a"):
            raise ValueError(
                f"`mode` should be either 'r' or 'a'. Received: {mode}"
            )
        self.close()
        self.h5_file = self._get_h5_file(
            self.path.with_name(filename), mode=mode
        )
        self._get_h5_group(self._h5_entry_path)
Switch to a different H5 file with the specified mode.

        This is useful for retrieving information from all shards, such as the
        total length, keys, and items.
        
keras/src/saving/saving_lib.py