1

I am trying to read in a series of DICOM files in a folder tree and I am using the below code to run through the tree, reading in each file as I go. The problem is I am getting IOErrors for files that definitely exist, I have checked file permissions and other SO threads such as Python: IOError: [Errno 2] No such file or directory but I haven't managed to get it working without these IOErrors yet. Does anyone have any ideas?

for root, dirs, files in os.walk(path):
      for fname in files:
        name = os.path.basename(os.path.abspath(fname))
        if name.startswith('.') == True:
          pass
        else:
          try:
            plan=dicom.read_file(fname)
            ds=dicom.read_file(fname, stop_before_pixels = True)

            kVp = TagChecker([0x18,0x60]) #KVP
            Target =  TagChecker([0x18,0x1191]) #ANODE
            Filter =  TagChecker([0x18,0x7050]) #

            write_results.writerow([Survey_Number, Patient_ID, View_Protocol, int(kVp), Target, Filter, Thickness, mAs_Exposure, LPad_Yes_No, autoorman, AECMode, AECDset, Patient_Age, Comment, Compression_Force])
            #print(fname)
          except IOError:
            print "IOError: ", "//" + os.path.join(root, fname) + "//"
          except InvalidDicomError:
          # This exception line prints an error message to the command line, checks to see if an error log
          # has been generated for this session, writes a new one if not and then writes the error to the log file
            print "Invalid Dicom File: ", fname
Community
  • 1
  • 1
James Jiimmy
  • 49
  • 1
  • 11
  • I have narrowed it down to something in the way I have coded the looping through the folders as the code throws an IOError even when pointing this to a single file, however when the loop is removed, it works fine. Can anyone help? – James Jiimmy Dec 01 '14 at 08:23

1 Answers1

2

Usually a method that takes a filename, like dicom.read_file(fname), will take an absolute filename (or assume that the filename is relative to the dir that your main python program is running in, the cwd()). Can I suggest that you put this line in front of the first read_file() call:

print "reading: %s" % os.path.abspath(fname)

Then you'll see the filename that you're actually trying to read. I'm guessing it's not the file (or droids) you think you're looking for.

In order to fix your problem.. join the dir and the fname before you read.. e.g.

full_fname = os.path.join(dir, fname)
dicom.read_file(full_fname)

In other words, I think you're reading files with relative paths and you want to be using absolute paths.

demented hedgehog
  • 7,007
  • 4
  • 42
  • 49
  • Thanks! It was joining the file name with the current directory path, i.e. the path the script is stored in not the path the file is stored in, all fixed now! For reference, it was just a simple case of adding the below: full_fname = os.path.join(root, fname) – James Jiimmy Dec 01 '14 at 09:05