python - 为什么这是else : pass needed for processing to continue?

标签 python python-2.7 if-statement continue

<分区>

有人可以解释为什么 else: pass为了执行其余代码(最终的 print 'processing... 语句)是否需要如下所示?注意 printelse放在那里只是为了让我知道执行确实在走那条路。

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

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)

解开谜题

看似奇怪的行为是由于缩进问题造成的,该问题在上面的代码中和我的文本编辑器中都不可见。原来是最后print语句缩进了 3 个空格,然后是一个制表符,这使得它看起来与 else 对齐, 但实际上它要么遵循 passelse如果它在那里,或者跟随 continueif 的第一部分.显然让我很困惑。

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

screenshot of file in my editor showing bad indentation

最佳答案

你不需要它。我运行了以下 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"

请注意,“处理”行没有差异。

关于python - 为什么这是else : pass needed for processing to continue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14900821/

相关文章:

python - 在 Django 中,不活动时自动注销的用户仍显示为事件使用

python - Flask 表单提交不起作用

python - If else 语句 - 如何跳过 else?

python - Tensorflow 变量未使用图间复制进行初始化

python - 系统托盘工具提示中的 PyQt RichText 格式化

python - 字符串中的日期时间对象, float 中的秒数

javascript - 如何在显示警报时尽量减少 if-else 语句

if-statement - 如何为销售人员字段制作公式字段?

python - 如何为使用 Django-CKEditor 上传的图像创建 uuid4 文件名?

python - Applescript 包和 Python