Search code examples
pythondirectorypydicom

Reading .dcm files from a nested directory


This is the form of my nested directory:

/data/patient_level/study_level/series_level/

For each patient_level folder, I need to read the ".dcm" files from the corresponding "series_level" folder.

How can I access the ".dcm" file in the "series_level" folders?

I need 3 features from a DICOM object.

This is my source code:

import dicom
record = dicom.read_file("/data/patient_level/study_level/series_level/000001.dcm")
doc = {"PatientID": record.PatientID, "Manufacturer": record.Manufacturer, "SeriesTime": record.SeriesTime}

Then, I will insert this doc to a Mongo DB.

Any suggestions is appreciated. Thanks in advance.


Solution

  • It is not quite clear the problem you are trying to solve, but if all you want is to get a list of all .dcm files from that the data directory, you can use pathlib.Path():

    from pathlib import Path
    
    data = Path('/data')
    list(data.rglob('*.dcm'))
    

    To split a file in its components, you can use something like this:

    dcm_file = '/patient_level/study_level/series_level/000001.dcm'
    _, patient_level, study_level, series_level, filename = dcm_file.split('/')
    

    Which would give you patient_level, study_level, series_level, and filename from dcm_file.

    But it would be better to stick with Path() and its methods, like this:

    dcm_file = Path('/patient_level/study_level/series_level/000001.dcm')
    dcm_file.parts
    

    Which would give you something like this:

    ('/', 'patient_level', 'study_level', 'series_level', '000001.dcm')
    

    Those are just starting points anyway.