处理文件路径 - Python 3.8

Handling File Pathing - Python 3.8

我有一个关于 glob 和文件路径的问题 - 假设我有一个

形式的静态文件路径

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-1990-MRI BREAST BILATERAL WWO-97538.000000-ax dyn pre-93877

我找到了解决我的问题的方法 - 使用 glob 我可以遍历一个文件夹和 return 一个包含所有给定文件夹的列表我可以 select 列表中的适当元素和考虑到患者后的内部文件路径数量是静态的(意味着每个患者中只有一个文件夹,然后是该结构中的更多文件夹)

我再次使用 glob 来匹配特定的内部文件夹,并将列表中的第一个元素用于 return 文件夹路径的字符串,然后使用字符串连接我通过使用 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 语句来检查正在 returned 的列表的大小。如果 glob 无法匹配第一种格式,我的列表将 return 长度为 0,我 运行 第二个匹配参数 return 一个大小大于 0 的数组,因此我可以再次使用列表中的第一个元素并从中获取文件路径。

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

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

希望这对以后的参考有所帮助。