擅长:python、mysql、java
<p>根据<code>os.walk</code>的<a href="http://docs.python.org/library/os.html#os.walk" rel="nofollow">documentation</a>:</p>
<blockquote>
<p>When <em>topdown</em> is <code>True</code>, the caller can modify the <em>dirnames</em> list in-place
(e.g., via <code>del</code> or slice assignment), and <code>walk()</code> will only recurse into the
subdirectories whose names remain in <em>dirnames</em>; this can be used to prune
the search, or to impose a specific order of visiting. Modifying
<em>dirnames</em> when <em>topdown</em> is <code>False</code> is ineffective, since the directories in
<em>dirnames</em> have already been generated by the time <em>dirnames</em> itself is
generated.</p>
</blockquote>
<p>所以理论上,如果清空<code>dirnames</code>,那么{<cd1>}将不会递归到任何其他目录。注意关于“…via del or slice assignment”的注释,您不能简单地执行<code>dirnames=[]</code>,因为这实际上不会影响<code>dirnames</code>列表的内容。在</p>