# Use Simpleitk to reading DICOM series In multiple folders

**URL:** <https://discourse.itk.org/t/use-simpleitk-to-reading-dicom-series-in-multiple-folders/4493>\
**Category:** Algorithms\
**Tags:** dicom, simpleitk, python\
**Created:** [October 13, 2021, 6:53am UTC](https://discourse.itk.org/t/use-simpleitk-to-reading-dicom-series-in-multiple-folders/4493 "2021-10-13T06:53:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kelly90802](https://discourse.itk.org/user_avatar/discourse.itk.org/kelly90802/32/2321_2.png) [@kelly90802](https://discourse.itk.org/u/kelly90802)\
**Post date:** [October 13, 2021, 6:53am UTC](https://discourse.itk.org/t/use-simpleitk-to-reading-dicom-series-in-multiple-folders/4493/1 "2021-10-13T06:53:41Z")

</div>

Hi all,  
Maybe someone here would know-  
I want to get the series IDs of each patient.  
the `sitk.ImageSeriesReader.GetGDCMSeriesIDs` need all dicom files are included in the `file_path`. But, what if the folder structure is:

```auto
contents

  folders1
    img1.dcm
    img2.dcm
    ...
  folders2
    img1.dcm
    img2.dcm
  folders ...
  img1.dcm
  img2.dcm
  ...

```

How can I get the Series IDs for each folder and save all Series IDs to a key ?

path\_home = ‘TCGA-train’  
path\_dir = os.path.join(path\_home,‘dataset’)  
print (path\_dir), len(path\_dir)  
paths = [path\_dir +"/"+ x for x in os.listdir(path\_dir)]

imgOriginal = {}  
print (paths)

for k in range(len(num\_count)):

```
reader = sitk.ImageSeriesReader()
filenamesDICOM = reader.GetGDCMSeriesFileNames(paths[k])
reader.SetFileNames(filenamesDICOM)
idx_slash_paths = paths[k].rfind('/')
idx_patient = paths[k][idx_slash_paths+1:]

imgOriginal[idx_patient] = reader.Execute()
print (idx_patient)
break

```

print (imgOriginal.keys())

---

<div class="post-metadata">

**Author:** ![zivy](https://discourse.itk.org/user_avatar/discourse.itk.org/zivy/32/1726_2.png) [@zivy](https://discourse.itk.org/u/zivy)\
**Post date:** [October 13, 2021, 12:47pm UTC](https://discourse.itk.org/t/use-simpleitk-to-reading-dicom-series-in-multiple-folders/4493/2 "2021-10-13T12:47:12Z")

</div>

Hello @kelly90802,

[This code](https://github.com/InsightSoftwareConsortium/SimpleITK-Notebooks/blob/03820ef9e79a2f78615a41625129ff3f17deba2c/Python/characterize_data.py#L238-L345) does what you want plus some other stuff. The `inspect_series` function traverses the directory structure and collects files into their respective study-series. Then the `inspect_single_series` reads a given series. Note that `inspect_single_series` accommodates repeated use of the same file names in a series so that there is no issue reading a series with files `folder1/img1.dcm` and `folder2/img1.dcm`.
