Why is this else: pass needed for processing to continue?

martineau picture martineau · Feb 15, 2013 · Viewed 17k times · Source

Can someone explain why the else: pass shown below is needed in order for the rest of the code (the final print 'processing... statement) to be executed? Note the print in the else was put there just so I could tell that execution was indeed taking that path.

It seems like that should happen whenever the continue isn't executed since code in the else does nothing. However, if I leave the else out, nothing further in the for loop appears to be executed when the condition is False -- when files with the extension do exist in the directory -- which doesn't make sense to me. The docs say continue "continues with the next cycle of the nearest enclosing loop", fine, but if one is not executed, shouldn't processing proceed to the next statement?

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)

Mystery Solved

The seemingly strange behavior was due to an indentation problem which is not visible in the code above nor normally in my text editor. It turned out that the last print statement was indented by 3 spaces then a tab, which makes it appear to align with the else, but in fact it either follows the pass in the else if it's there, or follows the continue in the first part of the if. Obviously confusing me a great deal.

Here's a screenshot of the code in my text editor with its "show space/tabs" option turned on. The red dots represent spaces and the red right guillemet (») represents a tab character:

screenshot of file in my editor showing bad indentation

Answer

mgilson picture mgilson · Feb 15, 2013

You don't need it. I ran the following 2 scripts:

#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)

and

#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)

I ran them as:

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

Here's the first couple lines of 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

Notice the presence of "processing" lines.

Then I diff the output:

diff junk.log junk.log2

with the following results:

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"

Note that there are no differences on the "processing" lines.