处理文件路径Python 3.8

2024-09-30 00:30:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个关于全局和文件路径的问题-假设我有一个静态文件路径,格式为

G:\ML_CDetector_ImageArchive\BreastCancer\RawFolder\Duke-Breast-Cancer-MRI\Duke-Breast-Cancer-MRI

我想在之后为文件夹添加变量路径,因为每个患者的文件夹命名约定不同,而患者文件夹约定是标准的,如:

Breast_MRI_XXX-其中x是1-922之间的一个数字(通过使用while循环,我能够处理这个数字),最后,内部文件夹变得有点古怪,但我再次无法通过glob使用以下通配符运算符来处理它:

f"{currentPatient}\\*\\现在,在文件夹中还有几个文件夹,我想输入这些文件夹,以便部分匹配名称:

gl.glob(f"{currentPatient}\\*\\[3rd]*\\*.dcm")但令我沮丧的是,我无法像以前那样获得正确的文件夹

for item in globInnerFolder:
                print(item)

我收到一张空白打印件,格式如下:

[]
[]
[]
[]
[]
[]
[]
[]
[]
[]

没有显示任何内容,也没有错误-我将如何执行以下操作-快速注意:我已尝试使用glob和iglob来查看是否返回了任何内容和nada:

    fpMainFolder        = gl.glob("G:\\ML_CDetector_ImageArchive\\BreastCancer\\RawFolder\\Duke-Breast-Cancer-MRI\\Duke-Breast-Cancer-MRI\\")
    # Loop Variable
    j = 0
    
    # while (j < len(csvList)):
    while (j < 10):
        # Image Configuration
        currentPatient      = str(csvList[j][0])
        # yStartPixel       = int(csvList[j][1])
        # yEndPixel         = int(csvList[j][2])
        # xStartPixel       = int(csvList[j][3])
        # xEndPixel         = int(csvList[j][4])
        # sliceStart        = int(csvList[j][5])
        # sliceEnd          = int(csvList[j][6])
        
        #imageSize          = (yEndPixel - yStartPixel), (xEndPixel - xStartPixel)
        
        # Patient Folder Loop Variable
        try:
            globInnerFolder     = gl.glob(f"{currentPatient}\\*\\[3rd]*\\*.dcm")
            print(globInnerFolder)
            for item in globInnerFolder:
                print(item)
        except:
            try:
                globInnerFolder = gl.iglob(f"{currentPatient}\\*\\[ph3ax]*\\*.dcm")
                for item in globInnerFolder:
                    print(item)
            except:
                print("Exiting - No File Structure Found")
                exit()

下面是一个完整文件路径的示例

G:\ML_CDetector_ImageArchive\BreastCancer\RawFolder\Duke-Breast-Cancer-MRI\Duke-Breast-Cancer-MRI\Breast_MRI_001\01-01-1990-MRI BREAST BILATERAL WWO-97538\3.000000-ax dyn pre-93877


Tags: 文件路径文件夹itemglobintprintmri
1条回答
网友
1楼 · 发布于 2024-09-30 00:30:04

我找到了解决问题的方法—使用glob,我可以遍历一个文件夹并返回一个包含所有给定文件夹的列表。使用这种方法,我可以从列表中选择适当的元素,并考虑到患者之后的内部文件路径数目是静态的(意味着每个患者体内只有一个文件夹,然后在该结构中有更多文件夹)

我再次使用glob来匹配特定的内部文件夹,并使用列表中的第一个元素返回文件夹路径的字符串,然后使用字符串连接,通过使用glob来匹配内部文件夹,我构建了一个动态文件夹搜索

    fpMainFolder        = "G:\\ML_CDetector_ImageArchive\\BreastCancer\\RawFolder\\Duke-Breast-Cancer-MRI\\Duke-Breast-Cancer-MRI\\"
    j = 0
    while j < len(csvList):
        currentPatient      = str(csvList[j][0])
        yStartPixel         = int(csvList[j][1])
        yEndPixel           = int(csvList[j][2])
        xStartPixel         = int(csvList[j][3])
        xEndPixel           = int(csvList[j][4])
        sliceStart          = int(csvList[j][5])
        sliceEnd            = int(csvList[j][6])
        fpPatientPath       = f"{currentPatient}"
        fpMriPass           = glob((fpMainFolder + fpPatientPath + "\\*\\" ))
        fpMriPass           = fpMriPass[0]
        fpThirdPass         = glob((fpMriPass + "\\*3rd*\\"))
        if len(fpThirdPass) == 0:
            fpThirdPass     = glob((fpMriPass + "\\*Ph3ax*\\"))
        print(fpThirdPass)
        
        for i in range((sliceEnd - sliceStart) + 1):
            try:
                fpDCMImage          = "1-" + "0" + str(sliceStart + i) + ".dcm"
                completeFp          = fpThirdPass[0] + fpDCMImage
                openDicom           = dcmread(completeFp)
                # Matplotlib -> Image Show -> Pixel Limits of X and Y -> Tumor Location
                plt.imsave("G:\\ML_CDetector_ImageArchive\\BreastCancer\\TrainingSet\\PatientImages\\TFJPEG_"      + currentPatient + "_TumourImageSlice"      + str(i) + ".jpg",
                           openDicom.pixel_array[yStartPixel:yEndPixel,xStartPixel:xEndPixel],
                           cmap=plt.cm.twilight,
                           origin = 'lower')
            except:
                try:
                    fpDCMImage          = "1-" + str(sliceStart + i) + ".dcm"
                    completeFp          = fpThirdPass[0] + fpDCMImage
                    openDicom           = dcmread(completeFp)
                    # Matplotlib -> Image Show -> Pixel Limits of X and Y -> Tumor Location
                    plt.imsave("G:\\ML_CDetector_ImageArchive\\BreastCancer\\TrainingSet\\PatientImages\\TFJPEG_"   + currentPatient + "_TumourImageSlice"      + str(i) + ".jpg",
                           openDicom.pixel_array[yStartPixel:yEndPixel,xStartPixel:xEndPixel],
                           cmap=plt.cm.twilight,
                           origin = 'lower')
                except Exception as e:
                    print(e)
        j += 1

在没有找到匹配项的情况下,我使用if语句检查返回的列表的大小。如果glob无法匹配我得到的第一种格式,列表将返回长度为0的,我运行第二个匹配参数,该参数将返回一个大小大于0的数组,因此我可以再次使用first元素并从中获取文件路径

        fpMriPass           = glob((fpMainFolder + fpPatientPath + "\\*\\" ))
        fpMriPass           = fpMriPass[0]
        fpThirdPass         = glob((fpMriPass + "\\*3rd*\\"))
        if len(fpThirdPass) == 0:
            fpThirdPass     = glob((fpMriPass + "\\*Ph3ax*\\"))
        print(fpThirdPass)

这可能不是最有效的解决方案,但它确实解决了我无法访问不遵循标准命名约定的文件夹的问题

我希望这有助于今后的参考

相关问题 更多 >

    热门问题