title: Python Recursive File System Search Function
body: <p>I'm new to this site and relatively new to Python.  I'm currently working on a project that requires me to write a recursive search function to look through files and directories.  The function takes a directory path, and search criteria, in this case, a file name, and is supposed to return the full path to the file, provided it exists.</p>
  
  <pre><code>def search(path, file):
  fullPath = path
  inDir = os.listdir(path)
  
  for element in inDir:
      if os.path.join(path, file) == os.path.join(path, element):
          fullPath = os.path.join(fullPath, file)
          print(fullPath)
          return fullPath
      elif os.path.isdir(os.path.join(path, element)):
          fullPath = search(os.path.join(path, element), file)
  </code></pre>
  
  <p>I know that recursion in Python requires a return statement on the recursive call, but I've also tried:</p>
  
  <pre><code>def search(path, file):
  fullPath = path
  inDir = os.listdir(path)
  
  for element in inDir:
      if os.path.join(path, file) == os.path.join(path, element):
          fullPath = os.path.join(fullPath, file)
          print(fullPath)
          return fullPath
      elif os.path.isdir(os.path.join(path, element)):
          return search(os.path.join(path, element), file)
  </code></pre>
  
  <p>and several other variations, but to no avail.  The first function prints the correct path, so I know that at some point in the recursion <code>fullPath</code> contains correct path, but I can't seem to get the function to return anything except <code>None</code>.</p>