【问题标题】:Handling File Pathing - Python 3.8处理文件路径 - Python 3.8
【发布时间】:2021-04-20 17:18:35
【问题描述】:

我有一个关于 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-01-1990-MRI BREAST BILATERAL WWO-97538\3.000000-ax dyn pre-93877

【问题讨论】:

    标签: python python-3.x glob


    【解决方案1】:

    我找到了解决问题的方法 - 使用 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 的数组,因此我可以再次使用列表中的第一个元素并从中获取文件路径。

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

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

    我希望这有助于作为未来的参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-12
      • 2020-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-19
      相关资源
      最近更新 更多