title: How do I search an existing file at maximum depth using recursion?
body: <p>I am trying to solve an exercise where I am supposed to search all the existing files present at different depths in a dirpath, and return a dictionary with the filenames as keys and their maximum depth as values.</p>
  <p>I have a rough idea of how it should be done, but the function I wrote gives the wrong output.</p>
  <p>Take a look at the following directory:</p>
  <pre><code>t4/.empty
  t4/u1
  t4/u1/.empty
  t4/u1/looney-tunes.txt
  t4/u1/v3
  t4/u1/v3/.empty
  t4/u1/v3/w4
  t4/u1/v3/w4/.empty
  t4/u1/v3/w4/x3
  t4/u1/v3/w4/x3/.empty
  t4/u1/v3/w4/x3/minnie.txt
  t4/u2
  t4/u2/.empty
  t4/u2/minnie.txt
  t4/u2/v3
  t4/u2/v3/.empty
  t4/u2/v3/looney-tunes.txt
  t4/u3
  t4/u3/.empty
  t4/u3/v1
  t4/u3/v1/.empty
  t4/u3/v1/minnie.txt
  t4/u3/v4
  t4/u3/v4/.empty
  t4/u3/v4/w1
  t4/u3/v4/w1/.empty
  t4/u3/v4/w1/looney-tunes.txt 
  </code></pre>
  <ul>
  <li>The empty files are to be ignored</li>
  <li>there are only two files that should be taken into account namely: 'looney-tunes.txt' and 'minnie.txt'.</li>
  <li>depth starts from 0 at the path given</li>
  </ul>
  <p>the function should return the following output:</p>
  <pre><code>{'looney-tunes.txt': 3, 'minnie.txt': 4}
  </code></pre>
  <p>but I get this output instead which is wrong because it's not taking into account that 'minnie.txt' is also also present at depth 4:</p>
  <pre><code> {'looney-tunes.txt': 3, 'minnie.txt': 2}
  </code></pre>
  <p>Here is my code:</p>
  <pre><code>def searchfiles(dir1,depth=0):
      files = {}
      for fname in os.listdir(dir1): 
          path = &quot;{}/{}&quot;.format(dir1,fname)
          if os.path.isfile(path) and not fname.startswith('.'): 
              if fname not in files:
                  files[fname] = depth 
          else:
              if os.path.isdir(path):
                  files.update(searchfiles(path,depth+1))
      return files
  </code></pre>