Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

loops - How to iterate through a directory and apply python script to multiple subfolders (not files) in that directory?

I am using a programme which converts dicom files stored in two different folders (input folders) to nifti format and saves them to a third folder (output folder). At the moment this programme requires me to manually specify the filepaths for these three folders.

I have a directory with multiple patients/subjects in it, each containing the two (input) folders. The third (output) folder does not exist but I have added os.makedirs to the above programme so it creates it.

I would like to create a script that can automate this process so that I don't have to manually specify the 3 filepaths for each patient. I want it to take each and every patient folder in the directory and apply the above - i.e read the dicom files in the two input folders for each patient, and then save the converted nifti files to a new folder for that specific patient.

#Importing libraries and modules:
import os
from dcmrtstruct2nii import dcmrtstruct2nii, list_rt_structs

#Next I want to create a for-loop that will walk through the directory patient folders/subfolders
#and that can then feed into the below section to automatically specify the RTstructdicom folder, the imagedicom folder
#and that can create a new folder called "Nifti" that can store the output.

for folderName, subfolders, filenames in os.walk('/Users/sh/Drive/folderC'):

    #**This is where I'm stuck**
   
#This next section will create the specified outputfolder e.g. 'Nifti',
#Then load the RTstruct dicom from the specified folder, 
#Then load the images dicoms from the specified folder,
#Then convert both to Nifti files saved in the specified output folder

RTstructdicom = ('/Users/sh/Drive/folderC/patient001/subfolder 1/RTSTRUCT_20210124_205145/RTSTRUCT/RTSTRUCT_20210124_205145.dcm')
imagedicoms = ('/Users/sh/Drive/folderC/patient001/subfolder 1/10022/DICOM')
outputfolder = ('/Users/sh/Drive/folderC/patient001/subfolder 1/10022/NIFTI')

print(list_rt_structs(RTstructdicom))
os.makedirs(outputfolder)
dcmrtstruct2nii(RTstructdicom, imagedicoms, outputfolder )

question from:https://stackoverflow.com/questions/65904916/how-to-iterate-through-a-directory-and-apply-python-script-to-multiple-subfolder

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not clear to me what you want to do with each these multiple subfolders.

I hope this example will get you started.

I did my best to

  • create an example directory tree
  • iterate through it with os.walk
  • do something to a particular subfolder

I suggest learning about the Path object from the pathlib: https://docs.python.org/3/library/pathlib.html#basic-use

I wanted to copy the contents of a subdirectory so I imported shutil.

This is the sample tree I created:

folderC
├── patient001
│   └── subfolder 1
│       └── RTSTRUCT_085211_328790
│           └── RTSTRUCT
│               └── RTSTRUCT_085211_328790
│                   └── RTSTRUCT_085211_328790.dcm
├── patient002
│   └── subfolder 2
│       └── RTSTRUCT_958381_489352
│           └── RTSTRUCT
│               └── RTSTRUCT_958381_489352
│                   └── RTSTRUCT_958381_489352.dcm
└── patient003
    └── subfolder 3
        └── RTSTRUCT_731792_968907
            └── RTSTRUCT
                └── RTSTRUCT_731792_968907
                    └── RTSTRUCT_731792_968907.dcm

I then used the following code to to something to some particular subfolders:

?

import os
from pathlib import Path
import shutil

for root, dirnames, _ in os.walk("folderC"):
    root_path = Path(root)
    for dirname in dirnames:
        dir_path = root_path / dirname
        # Use conditionals at this point to select which directory you want to work with.
        # For example.
        # make a destination folder in all folders with "subfolder" in the name
        if "subfolder" in dir_path.name:
            destination = (dir_path / "10022" / "NIFTI")
            destination.mkdir(exist_ok=True, parents=True)
        # An example that copies the files from the deepest "RTSTRUCT_xxxx_xxxx" folder
        # to the destination
        if "RTSTRUCT_" in dir_path.name and dir_path.parent.name == "RTSTRUCT":
            shutil.copytree(dir_path, destination, dirs_exist_ok=True)

This is the result. You can see the files from the "RSTRUCT_xxx_xxxx" directory is in the nearly created one inside of the "subfolder x" directory.

folderC
├── patient001
│   └── subfolder 1
│       ├── 10022
│       │   └── NIFTI
│       │       └── RTSTRUCT_085211_328790.dcm
│       └── RTSTRUCT_085211_328790
│           └── RTSTRUCT
│               └── RTSTRUCT_085211_328790
│                   └── RTSTRUCT_085211_328790.dcm
├── patient002
│   └── subfolder 2
│       ├── 10022
│       │   └── NIFTI
│       │       └── RTSTRUCT_958381_489352.dcm
│       └── RTSTRUCT_958381_489352
│           └── RTSTRUCT
│               └── RTSTRUCT_958381_489352
│                   └── RTSTRUCT_958381_489352.dcm
└── patient003
    └── subfolder 3
        ├── 10022
        │   └── NIFTI
        │       └── RTSTRUCT_731792_968907.dcm
        └── RTSTRUCT_731792_968907
            └── RTSTRUCT
                └── RTSTRUCT_731792_968907
                    └── RTSTRUCT_731792_968907.dcm

There are some simple examples in the Python documentation as well: https://docs.python.org/3/library/os.html#os.walk


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...