【问题标题】:FileNotFoundError, but file DOES EXIST [duplicate]FileNotFoundError,但文件确实存在[重复]
【发布时间】:2021-09-18 09:55:12
【问题描述】:

我正在尝试在我的 Jupyter Notebook 中加载 .dcm 文件,但我收到 FileNotFound 错误,即使文件确实存在!我通过在 DICOM 查看器中打开它进行了检查。代码很简单:

image = pydicom.dcmread(dicom_filename).pixel_array

这是错误信息:

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\StefanCepa995\\Desktop\\Machine Learning\\Projects\\Computer Vision\\Breast Mammography Analysis\\Data\\CBIS-DDSM\\dicom_files\\Mass-Training_Full_Mammogram_Images\\Mass-Training_P_00001_LEFT_CC\\07-20-2016-DDSM-74994\\1.000000-full mammogram images-24515\\1-1.dcm'

有趣的是,我尝试列出 C:\\Users\\StefanCepa995\\Desktop\\Machine Learning\\Projects\\Computer Vision\\Breast Mammography Analysis\\Data\\CBIS-DDSM\\dicom_files\\ 目录中的所有文件,os.walk() 列出所有 .dcm 文件,但是当我在其中一个文件上使用 os.path.exists() 时,我得到 False 作为返回值:

for root, dirs, files in os.walk(cbis_ddsm_dicom_mass_train_dir):
    for f in files:
        file_path = os.path.join(root, f)
        print(file_path)
        print(os.path.exists(file_path))

C:\Users\StefanCepa995\Desktop\Machine Learning\Projects\Computer Vision\Breast Mammography Analysis\Data\CBIS-DDSM\dicom_files\Mass-Training_Full_Mammogram_Images\Mass-Training_P_00001_LEFT_CC\07-20-2016-DDSM-74994\1.000000-full mammogram images-24515\1-1.dcm
False
C:\Users\StefanCepa995\Desktop\Machine Learning\Projects\Computer Vision\Breast Mammography Analysis\Data\CBIS-DDSM\dicom_files\Mass-Training_Full_Mammogram_Images\Mass-Training_P_00001_LEFT_MLO\07-20-2016-DDSM-90988\1.000000-full mammogram images-80834\1-1.dcm
False
C:\Users\StefanCepa995\Desktop\Machine Learning\Projects\Computer Vision\Breast Mammography Analysis\Data\CBIS-DDSM\dicom_files\Mass-Training_Full_Mammogram_Images\Mass-Training_P_00004_LEFT_CC\07-20-2016-DDSM-95697\1.000000-full mammogram images-46540\1-1.dcm
False
C:\Users\StefanCepa995\Desktop\Machine Learning\Projects\Computer Vision\Breast Mammography Analysis\Data\CBIS-DDSM\dicom_files\Mass-Training_Full_Mammogram_Images\Mass-Training_P_00004_LEFT_MLO\07-20-2016-DDSM-20939\1.000000-full mammogram images-00162\1-1.dcm
False

【问题讨论】:

标签: python python-3.x windows pydicom


【解决方案1】:

以下是可能的选项:您输入文件路径的方式可能与 Windows 不兼容。这是一个帮助解决此问题的链接:Windows path in Python

另外,我发现你可以用两行代码轻松搜索一个文件,而且它的时间复杂度比使用嵌套循环要少。这是我在自己的代码中尝试过的:

    import os.path

    # stores whether given file exists or not
    result = os.path.isfile(r'C:\Users\jindu\Coding\squid_fram1.png')
    print(result)

我从这个网站得到了这个参考:https://careerkarma.com/blog/python-check-if-file-exists/

【讨论】:

  • 感谢您的评论。不幸的是不是我想要的。上面的人回答了我的问题。
猜你喜欢
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-19
  • 2016-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多