title: Travel directory tree with limited recursion depth
body: <p>I need to process all files in a directory tree recursively, but with a limited depth. </p>
  
  <p>That means for example to look for files in the current directory and the first two subdirectory levels, but not any further. In that case, I must process e.g. <code>./subdir1/subdir2/file</code>, but not <code>./subdir1/subdir2/subdir3/file</code>.</p>
  
  <p>How would I do this best in Python 3?</p>
  
  <p>Currently I use <code>os.walk</code> to process all files up to infinite depth in a loop like this:</p>
  
  <pre><code>for root, dirnames, filenames in os.walk(args.directory):
      for filename in filenames:
          path = os.path.join(root, filename)
          # do something with that file...
  </code></pre>
  
  <p>I could think of a way counting the directory separators (<code>/</code>) in <code>root</code> to determine the current file's hierarchical level and <code>break</code> the loop if that level exceeds the desired maximum.</p>
  
  <p>I consider this approach as maybe insecure and probably pretty inefficient when there's a large number of subdirectories to ignore. What would be the optimal approach here?</p>