如何迭代目录并将Python脚本应用于该目录中的多个子文件夹(而不是文件)?

问题描述 投票:0回答:2

我正在使用一个程序,它将存储在两个不同文件夹(输入文件夹)中的 dicom 文件转换为 nifti 格式,并将它们保存到第三个文件夹(输出文件夹)。目前该程序要求我手动指定这三个文件夹的文件路径。

我有一个目录,其中包含多个患者/受试者,每个目录包含两个(输入)文件夹。第三个(输出)文件夹不存在,但我已将 os.makedirs 添加到上面的程序中,以便它创建它。

我想创建一个可以自动执行此过程的脚本,这样我就不必为每个患者手动指定 3 个文件路径。我希望它获取目录中的每个患者文件夹并应用上述内容 - 即读取每个患者的两个输入文件夹中的 dicom 文件,然后将转换后的 nifti 文件保存到该特定患者的新文件夹中。

#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 )

python loops directory-structure os.walk python-os
2个回答
1
投票

我不清楚你想对这些多个子文件夹做什么。

我希望这个例子能让您入门。

我已经尽力了

  • 创建示例目录树
  • 使用
    os.walk
  • 迭代它
  • 对特定子文件夹执行某些操作

我建议从

Path
了解
pathlib
对象:https://docs.python.org/3/library/pathlib.html#basic-use

我想复制子目录的内容,所以我导入了shutil。

这是我创建的示例树:

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

然后我使用以下代码将某些内容复制到某些特定的子文件夹中:

ufeff

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)

这就是结果。您可以看到“RSTRUCT_xxx_xxxx”目录中的文件位于“subfolder x”目录中即将创建的目录中。

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

Python 文档中也有一些简单的示例:https://docs.python.org/3/library/os.html#os.walk


0
投票

正如 @dmmfll 所提到的,您可以修改脚本以迭代每个患者的文件夹、识别输入文件夹并为 NIfTI 文件创建输出文件夹。该循环将遍历每个患者目录,构建 RTstruct 和图像 DICOM 的路径,创建 NIfTI 文件夹,然后执行转换。所以基本上,答案是使用 os.walk 来考虑整个层次结构。

应该是这样的:

import os
from dcmrtstruct2nii import dcmrtstruct2nii

base_dir = '/Users/sh/Drive/folderC'

for patient_id in next(os.walk(base_dir))[1]:
    patient_path = os.path.join(base_dir, patient_id)
    rtstruct_path = os.path.join(patient_path, 'RTSTRUCT.dcm')
    image_dicoms_path = os.path.join(patient_path, 'DICOM')
    output_folder = os.path.join(patient_path, 'NIFTI')
    
    os.makedirs(output_folder, exist_ok=True)
    dcmrtstruct2nii(rtstruct_path, image_dicoms_path, output_folder)
© www.soinside.com 2019 - 2024. All rights reserved.