mar*_*eau 3 python if-statement continue python-2.7
有人可以解释为什么else: pass需要执行以下所示的代码print 'processing...才能执行其余代码(最终语句)?请注意,print在else那里放置,所以我可以告诉执行确实采取了这条道路.
似乎应该在continue没有执行时发生,因为代码中else没有任何操作.但是,如果我离开else了,没有什么可以再在for环似乎被执行时,条件为False -当与扩展名的文件做这没有任何意义,我-在目录中.文档说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)
Run Code Online (Sandbox Code Playgroud)
谜团已揭开
看似奇怪的行为是由于缩进问题,这在上面的代码中也看不到,通常在我的文本编辑器中也是如此.事实证明,最后一个print语句缩进了3个空格然后是一个制表符,这使得它看起来与它对齐else,但实际上它要么跟pass在elseif中,要么跟在它的continue第一部分中if.显然让我很困惑.
这是我的文本编辑器中的代码截图,其中显示了"show space/tabs"选项.红点代表空格,红色>>代表制表符:

你不需要它.我运行了以下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)
Run Code Online (Sandbox Code Playgroud)
和
#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)
Run Code Online (Sandbox Code Playgroud)
我把他们当作:
python test1.py > junk.log
python test2.py > junk.log2
Run Code Online (Sandbox Code Playgroud)
这是前几行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
Run Code Online (Sandbox Code Playgroud)
注意存在"处理"线.
然后我diff输出:
diff junk.log junk.log2
Run Code Online (Sandbox Code Playgroud)
结果如下:
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"
Run Code Online (Sandbox Code Playgroud)
请注意,"处理"行没有区别.
我将回答我自己的问题并最终接受它。所描述的看似奇怪的行为是由一个微妙的缩进问题引起的,用户@delnan 首先引起了我的注意。因为是隐形的,一开始我觉得不可能,后来经过多方调查才发现。其中的详细信息已添加到我的问题的末尾。
| 归档时间: |
|
| 查看次数: |
10172 次 |
| 最近记录: |