path1='C:/Users/Desktop/train/'

def load_data(path):
  my_dir = sorted(os.listdir(path))
  data1=[]
  gt=[]
  for p in tqdm(my_dir):
    data_list = os.path.join(path+p)
    img = sitk.ReadImage(path + p + '/'+'seg.nii.gz')    
    seg =  sitk.GetArrayFromImage(img)
    img = sitk.ReadImage(path + p + '/'+ 'T2W.nii.gz')
    T2W = sitk.GetArrayFromImage(img)
    seg = np.pad(seg, [(23-seg.shape[0], 0), (0, 0), (0, 0)]) # pads in the 0th dimension
    T2W = np.pad(T2W, [(23-T2W.shape[0], 0), (0, 0), (0, 0)])
    data1.append([T2W])
    gt.append(seg)

  data1 = np.asarray(data1,dtype=np.uint8)
  gt = np.asarray(gt,dtype=np.uint8)
  return data1,gt

data1,gt1 = load_data(path1)

Can you try this please ?