title: Fast folder search within directory [Python]
body: <p>Situation: A lot of files and folders into the main directory, very old work computers, old python 2.something(Software bound so nothing to be done for now), all the data is on a network drive.</p>
  <p>I have a variable which contains the folder name it is supposed to find, there are like 2-4 of these withing each project. For ShotName = &quot;DVX_010_400_BG_V001&quot;</p>
  <p>Expected output:</p>
  <pre><code>C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\3dcomp\DVX_010_400_BG_V001
  
  C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\input\DVX_010_400_BG_V001
  
  C:\Users\Dame\Desktop\NUKESTUFF\TPN\FilmLance\DVX\render\DVX_010_400_BG_V001
  </code></pre>
  <p>Goal: to find the folders that have the same name as the ShotName variable and return their path</p>
  <p>Broblem: Current code is too slow and it hangs.</p>
  <p>in my desktop env, the main folder is on dekstop (fast NVME ssd), the fastest processor, modern python so it's fast, like 2 seconds. My best code:</p>
  <pre><code>import os
  
  ShotName = &quot;DVX_010_400_BG_V001&quot;
  head_dir= &quot;C:\\Users\\Dame\\Desktop\\NUKESTUFF\\TPN\\&quot;
  
  
  def dir_list_folder(head_dir, ShotName):
      &quot;&quot;&quot;Return a list of the full paths of the subdirectories
      under directory 'head_dir' named 'ShotName'&quot;&quot;&quot;
      dirList = []
      for fn in os.listdir(head_dir):
          dirfile = os.path.join(head_dir, fn)
          if os.path.isdir(dirfile):
              print (dirfile)
              if fn.upper() == ShotName.upper():
                  dirList.append(dirfile)
              else:
                  dirList += dir_list_folder(dirfile, ShotName)
  
  
      return dirList
  
  if __name__ == '__main__':
      for item in dir_list_folder(head_dir, ShotName):
          # print (item)
  
          if &quot;render&quot; in item :
              ProjectPath = item.split(&quot;\\&quot;)[:-2]
              ProjectPath = &quot;/&quot;.join(ProjectPath)
              print (f&quot;ProjectPath = {ProjectPath}&quot;)
  </code></pre>