code: def restore_directory_state(self, fname):
          
          root = osp.normpath(to_text_string(fname))
          if not osp.exists(root):
              # Directory has been (re)moved outside Spyder
              return
          for basename in os.listdir(root):
              path = osp.normpath(osp.join(root, basename))
              if osp.isdir(path) and path in self.__expanded_state:
                  self.__expanded_state.pop(self.__expanded_state.index(path))
                  if self._to_be_loaded is None:
                      self._to_be_loaded = []
                  self._to_be_loaded.append(path)
                  self.setExpanded(self.get_index(path), True)
          if not self.__expanded_state:
              self.fsmodel.directoryLoaded.disconnect(self.restore_directory_state)
docs: Restore directory expanded state