Python的os.walk错误?

Mik*_*ron 0 python

os.walk文档(http://docs.python.org/library/os.html?突显= os.walk#os.walk),说我可以跳过通过从目录列表中删除它们不需要遍历目录.来自文档的明确示例:

import os
from os.path import join, getsize
for root, dirs, files in os.walk('python/Lib/email'):
    print root, "consumes",
    print sum(getsize(join(root, name)) for name in files),
    print "bytes in", len(files), "non-directory files"
    if 'CVS' in dirs:
        dirs.remove('CVS')  # don't visit CVS directories
Run Code Online (Sandbox Code Playgroud)

我看到不同的行为(使用ActivePython 2.6.2).即代码:

>>> for root,dirs,files in os.walk(baseline):
...     if root.endswith(baseline):
...             for d in dirs:
...                     print "DIR: %s" % d
...                     if not d.startswith("keep_"):
...                             print "Removing %s\\%s" % (root,d)
...                             dirs.remove(d)
...
...     print "ROOT: %s" % root
...
Run Code Online (Sandbox Code Playgroud)

我得到输出:

DIR: two
Removing: two
DIR: thr33
Removing: thr33
DIR: keep_me
DIR: keep_me_too
DIR: keep_all_of_us
ROOT: \\mach\dirs
ROOT: \\mach\dirs\ONE
ROOT: \\mach\dirs\ONE\FurtherRubbish
ROOT: \\mach\dirs\ONE\FurtherRubbish\blah
ROOT: \\mach\dirs\ONE\FurtherRubbish\blah\Extracted
ROOT: \\mach\dirs\ONE\FurtherRubbish\blah2\Extracted\Stuff_1
...
Run Code Online (Sandbox Code Playgroud)

WTF?为什么没有\\mach\dirs\ONE删除?它显然不是以"keep_"开头.

Sil*_*ost 5

因为您在dirs迭代时修改列表.ONE刚被跳过,从未被人看过.相比:

>>> a = [1, 2, 3]
>>> for i in a:
    if i > 1:
        a.remove(i)


>>> a
[1, 3]
Run Code Online (Sandbox Code Playgroud)