【问题标题】:How to iterate through a directory and apply python script to multiple subfolders (not files) in that directory?如何遍历目录并将python脚本应用于该目录中的多个子文件夹(不是文件)?
【发布时间】:2021-01-26 16:10:13
【问题描述】:

我正在使用一个程序,它将存储在两个不同文件夹(输入文件夹)中的 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


    【解决方案1】:

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

    我希望这个例子能让你开始。

    我尽力了

    • 创建示例目录树
    • os.walk遍历它
    • 对特定子文件夹执行操作

    我建议从pathlib 了解Path 对象: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
    

    然后我使用以下代码对某些特定的子文件夹进行处理:

    

    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”目录中的文件位于“子文件夹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

    【讨论】:

    • 非常感谢您的回复。对不起,如果我没有把 OP 说清楚。我想对子文件夹做的是让它们插入转换工具。目前我需要在代码中手动输入路径名 - 例如说明在哪里查找 RTstructdicom、imagedicoms 和 outputfolder。我想做的是创建一个可以为每位患者自动执行此操作的代码。
    最近更新 更多