Python, opening files in a loop (dicom)

I am currently reading 200 dicom images using code:

ds1 = dicom.read_file('1.dcm')

so far this has worked, but I am trying to make my code shorter and easier to use by creating a loop to read in files using this code:

for filename in os.listdir(dirName):
    dicom_file = os.path.join("/",dirName,filename)   
    exists = os.path.isfile(dicom_file) 
    print filename
    ds = dicom.read_file(dicom_file)

This code is currently not working and I am getting an error:

"raise InvalidDicomError("File is missing 'DICM' marker. "
dicom.errors.InvalidDicomError: File is missing 'DICM' marker. Use         
force=True to force reading

Can someone advise me where I am wrong, please?

+4
source share
2 answers

I think the line is:

dicom_file = os.path.join("/",dirName,filename) 

maybe a problem? He will join all three to form a path rooted in '/'. For example:

os.path.join("/","directory","file")

will give you "/ directory / file" (absolute path), and:

os.path.join("directory","file")

will provide you with a "directory / file" (relative path)

, , , "*.dcm", glob:

import glob

files_with_dcm = glob.glob("*.dcm")

:

import glob

files_with_dcm = glob.glob("/full/path/to/files/*.dcm")

os.listdir(dirName) , , whatnot

exists = os.path.isfile(dicom_file) , "if exists:".

glob, , :

if exists:
   try:
      ds = dicom.read_file(dicom_file)
   except InvalidDicomError as exc:
      print "something wrong with", dicom_file

/, : , ...

+3

:

dicom_file = os.path.join("/",dirName,filename) 
if not dicom_file.endswith('.dcm'):
    continue 
+2

Source: https://habr.com/ru/post/1613688/


All Articles