title: Python: how to search for specific "string" in directory name (not individual file names)
body: <p>I want to create a list of all the <strong><em>filepath names</em></strong> that match a specific string e.g. "04_DEM" so I can do further processing on the files inside those directories?</p>
  
  <p>e.g. </p>
  
  <p>INPUT</p>
  
  <pre><code> C:\directory\NewZealand\04DEM\DEM_CD23_1232.tif
   C:\directory\Australia\04DEM\DEM_CD23_1233.tif
   C:\directory\NewZealand\05DSM\DSM_CD23_1232.tif
   C:\directory\Australia\05DSM\DSM_CD23_1232.tif
  </code></pre>
  
  <p>WANTED OUTPUT</p>
  
  <pre><code> C:\directory\NewZealand\04DEM\
   C:\directory\Australia\04DEM\
  </code></pre>
  
  <p>This makes sure that only those files are processed, as some other files in the directories also have the same string "DEM" included in their filename, which I do not want to modify.</p>
  
  <p>This is my bad attempt due to being a rookie with Py code </p>
  
  <pre><code> import os
  
   for dirnames in os.walk('D:\Canterbury_2017Copy'):
       print dirnames
       if dirnames=='04_DEM' &gt; listofdirectoriestoprocess.txt
  
   print "DONE CHECK TEXT FILE"
  </code></pre>