简体   繁体   English

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

[英]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).我正在使用一个程序,它将存储在两个不同文件夹(输入文件夹)中的 dicom 文件转换为 nifti 格式并将它们保存到第三个文件夹(输出文件夹)。 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.第三个(输出)文件夹不存在,但我已将 os.makedirs 添加到上述程序中,以便创建它。

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.我想创建一个可以自动执行此过程的脚本,这样我就不必为每个患者手动指定 3 个文件路径。 I want it to take each and every patient folder in the directory and apply the above - ie 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.我希望它获取目录中的每个患者文件夹并应用上述内容 - 即为每个患者读取两个输入文件夹中的 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 )

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.walkos.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我建议从 pathlib 中了解Path pathlibhttps://docs.python.org/3/library/pathlib.html#basic-use

I wanted to copy the contents of a subdirectory so I imported shutil.我想复制一个子目录的内容,所以我导入了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.您可以看到“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

There are some simple examples in the Python documentation as well: https://docs.python.org/3/library/os.html#os.walk Python 文档中也有一些简单的示例: https://docs.python.org/3/library/os.html#os.walk

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历文件夹中的文件并将我的脚本应用于python中的所有文件 - How to iterate through files in a folder and apply my script to all in python 如何遍历目录中的子目录并计算python中子目录中的文件 - How to iterate through subdirectories in a directory and count the files in the subdirectories in python 尝试遍历目录中的 .wav 文件(Python) - Trying to iterate through .wav files in a directory (Python) 如何遍历指定的每个目录并在文件上运行命令(Python) - How to iterate through every directory specified and run commands on files (Python) 如何使用目录来遍历目录中的文件并将INFO插入MySQL数据库 - How to iterate through files in a directory and plug the INFO into a MySQL database with PYTHON Python-遍历目录中的子文件夹和文件,而不会忽略该子文件夹 - Python - loop through subfolders and files in a directory without ignoring the subfolder 如何从同一目录中的多个特定子文件夹中移动文件? - How to move files from multiple specific subfolders in the same directory? 如何在Python目录和子文件夹中获取文件的某些列表(5)? - How do I get some(5) list of files in a directory and subfolders in Python? 如何计算特定目录中的文件数,而不扫描 python 中的子文件夹 - how to count number of files in a particular directory, without scanning subfolders in python 如何遍历具有多个文件夹的目录中的文件,对文件进行操作,保存到不同的文件夹集 - how to iterate through files in directory with multiple folders, operate on files, save to different set of folders
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM