微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

处理文件路径 - Python 3.8

如何解决处理文件路径 - 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-01-1990-MRI BREAST BILAteraL WWO-97538\3.000000-ax dyn pre-93877

解决方法

我找到了解决我的问题的方法 - 使用 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",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)

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。