5

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!

user3368693
  • 73
  • 1
  • 5
  • 1
    Is it because you have dirs defined twice? Once as ``dirs = []`` and again in ``for subdir, dirs, files in os.walk(`` Try change ``dirs = []`` to a something like ``found = []`` and then also change ``dirs.append`` to ``found.append`` – Steve Oct 02 '14 at 23:45
  • @Steve is right. os.walk uses the dirs list to keep iterating. You can add and remove stuff to change what is looked at next. You keep adding cur, so it keeps processing cur. – tdelaney Oct 02 '14 at 23:47
  • Thank you guys. I won't be writing code while sick again! – user3368693 Oct 03 '14 at 19:33

1 Answers1

3

You are modifying dirs, which is used by os.walk to figure out which directory to recurse in next. Create a separate variable to store your results in.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88