0

I am trying to load .dcm file in my Jupyter Notebook, but I am getting FileNotFound error, even though file DOES exist! I've checked by opening it in the DICOM viewer. Code is pretty simple:

image = pydicom.dcmread(dicom_filename).pixel_array

Here is the error message:

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'

Funny part is, I've tried listing all files in C:\\Users\\StefanCepa995\\Desktop\\Machine Learning\\Projects\\Computer Vision\\Breast Mammography Analysis\\Data\\CBIS-DDSM\\dicom_files\\ directory, and os.walk() lists all .dcm files, but when I use os.path.exists() on one of those files I am getting False as return value:

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
Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
CodingAddict
  • 181
  • 2
  • 15
  • 3
    There was something with a path limit of 260 characters in Windows: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file and https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=cmd – Matthias Jul 07 '21 at 21:18
  • Which is a bit funny because the one file-path I looked at, is of length 261 :-) – ShlomiF Jul 07 '21 at 21:20
  • Have I mentioned that I hate Windows? Thanks guys! – CodingAddict Jul 07 '21 at 21:57

1 Answers1

0

Here are possible options: the way you entered your file path may not be compatible with Windows'. Here is a link to help with that: Windows path in Python.

In addition, I found that you could search for a file easily with two lines of code, and it would be less time-complex than using nested loops. Here is what I tried in my own code:

    import os.path

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

I got this reference from this site: https://careerkarma.com/blog/python-check-if-file-exists/

Dharman
  • 30,962
  • 25
  • 85
  • 135
Avaruto
  • 21
  • 5