5

有人可以解释为什么else: pass需要下面显示的内容才能执行其余代码(最终print 'processing...语句)吗?注意printinelse被放在那里只是为了让我知道执行确实是在走这条路。

似乎只要continue不执行 就应该发生这种情况,因为 中的代码else什么都不做。但是,如果我离开了,当条件为 False 时,循环中似乎不会执行else任何进一步的操作——当目录中确实存在具有扩展名的文件时——这对我来说没有意义。文档说“继续最近的封闭循环的下一个循环”,很好,但是如果没有执行,不应该处理继续下一条语句吗?forcontinue

import os

source_dir = r'C:\Downloads'
ext = '.mp3'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    else:  # why is this clause needed to continue this iteration of a loop?
        print 'contains   "{}"'.format(dirName)
        pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

谜团已揭开

看似奇怪的行为是由于缩进问题,这在上面的代码中不可见,通常在我的文本编辑器中也不可见。事实证明,最后一个语句缩进了 3 个空格,然后是一个制表符,这print使它看起来与. 显然让我很困惑。elsepasselsecontinueif

这是我的文本编辑器中的代码截图,它的“显示空间/标签”选项已打开。红点代表空格,红色右 guillemet ( ») 代表制表符:

我的编辑器中显示错误缩进的文件屏幕截图

4

2 回答 2

6

你不需要它。我运行了以下 2 个脚本:

#test1.py
import os

source_dir = '.'
ext = '.txt'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    else:  # why is this clause needed to continue this iteration of a loop?
        print 'contains   "{}"'.format(dirName)
        pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

#test2.py
import os

source_dir = '.'
ext = '.txt'

for dirName, subdirList, fileList in os.walk(source_dir):
    if not any(os.path.splitext(fileName)[1].lower() == ext for fileName in fileList):
        print '  skipping "{}"'.format(dirName)
        continue
    #else:  # why is this clause needed to continue this iteration of a loop?
    #    print 'contains   "{}"'.format(dirName)
    #    pass

    print 'processing "{}" which has "{}" files'.format(dirName, ext)

我将它们运行为:

python test1.py > junk.log
python test2.py > junk.log2

这是前几行junk.log

test $ head junk.log
processing "." which has ".txt" files
  skipping "./new"
  skipping "./unum"
processing "./unum/kiv-unum-409befe069ac" which has ".txt" files
  skipping "./unum/kiv-unum-409befe069ac/build"
  skipping "./unum/kiv-unum-409befe069ac/build/bdist.macosx-10.3-fat"
  skipping "./unum/kiv-unum-409befe069ac/build/lib"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/tests"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/unum"
  skipping "./unum/kiv-unum-409befe069ac/build/lib/unum/units

注意“处理”行的存在。

然后我diff输出:

diff junk.log junk.log2

结果如下:

0a1
> contains   "."
3a5
> contains   "./unum/kiv-unum-409befe069ac"
14a17
> contains   "./unum/kiv-unum-409befe069ac/docs"
16a20
> contains   "./unum/kiv-unum-409befe069ac/nose-1.2.1-py2.7.egg/EGG-INFO"
19a24
> contains   "./unum/kiv-unum-409befe069ac/nose-1.2.1-py2.7.egg/nose"
30a36
> contains   "./unum/kiv-unum-409befe069ac/Unum.egg-info"

请注意,“处理”行没有区别。

于 2013-02-15T18:20:33.900 回答
5

我将回答我自己的问题并最终接受它。所描述的看似奇怪的行为是由一个微妙的缩进问题引起的,用户@delnan 首先引起了我的注意。因为它是隐形的,所以一开始我觉得不会是这样,但经过更多的调查最终发现了它。其中的详细信息已添加到我的问题的末尾。

于 2013-02-15T22:00:39.810 回答