如何遍历目录并将python脚本应用于该目录中的多个子文件夹不是文件?

问题描述

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

解决方法

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

我希望这个例子能帮助你入门。

我尽力了

  • 创建示例目录树
  • 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

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

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