import SimpleITK as sitk from glob import glob import os path = glob(r"D:\MyData\date\*") n = len(path) res = [] for file in path: # print(file) file_name = os.path.join(os.path.join(file, "image"), "sample.nii") im = sitk.ReadImage(file_name) image = sitk.GetArrayFromImage(im) res.append(image.shape[0]) print(image.shape) print(sorted(res))