I have a folder which has subfolders containing 1000s of DICOM images which I want to read in from IDLE and analyze.

I have used the following code to find file paths:

import sys           
print sys.path

I subsequently tried placing my folder which I want to access in these file paths, however I still can not access the files and I get the following error:

>>> fp = open(fp, 'rb')
IOError: [Errno 2] No such file or directory: 'IM-0268-0001.dcm' 

I have also tried:

sys.path.insert(0, 'C:/desktop/James_Phantom_CT_Dec_16th/Images')

But this did not work for me either. Help much appreciated, very frustrated.

(using Python 2.7, 64 bit windows OS).

有帮助吗?

解决方案

When opening a file, Python does not search the path. You must specify the full path to open:

d = 'C:/desktop/James_Phantom_CT_Dec_16th/Images'
fp = open(d +'IM-0268-0001.dcm',"rb")

Edit: d is the string that will hold the path so that you don't have to re-type it for each file. fp will hold the file object that you will work with. The "rb" is the way you want to open the file:
r - read
w - write with truncate
a - append
r+ - read and write
Also, if working in windows, add "b" to work with binary files. See here.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top