I am trying to write a script that navigates through a directory and identifies DICOM files with a specified piece of header information. After I have found a file that meets the criteria, I want to add the directory of the file to a list if it hasn't so already.
import dicom
import os
import os.path
import re
param = "PatientID"
val = "7rAgWJ."
rootDir = "C:\\work\\SJM\\WRIX"
dirs = []
i = 0
for subdir, dirs, files in os.walk(rootDir, topdown = True):
for f in files:
try:
f = os.path.join(subdir, f)
ds = dicom.read_file(f)
attr = getattr(ds, param)
if attr == val:
cur = os.path.dirname(os.path.realpath(f))
if cur not in dirs:
dirs.append(cur);
My problem is my code never terminates because my outer for loop runs continuously. The loop cycles through the files in one of the directories in C:\work\SJM\WRIX\, but never "moves on" to the next folder.
Thanks!