python - Why is this else: pass needed for processing to continue? -
can explain why else:pass
shown below needed in order rest of code (the final print 'processing...
statement) executed? note print
in else
set there tell execution indeed taking path.
it seems should happen whenever continue
isn't executed since code in else
nothing. however, if leave else
out, nil farther in for
loop appears executed when status false -- when files extension do exist in directory -- doesn't create sense me. docs continue
"continues next cycle of nearest enclosing loop", fine, if 1 not executed, shouldn't processing should proceed next statement?
import os source_dir = r'c:\downloads' ext = '.mp3' dirname, subdirlist, filelist in os.walk(source_dir): if not any(os.path.splitext(filename)[1].lower() == ext filename in filelist): print ' skipping "{}"'.format(dirname) go on else: # why clause needed go on iteration of loop? print 'contains "{}"'.format(dirname) pass print 'processing "{}" has "{}" files'.format(dirname, ext)
mystery solved
the seemingly unusual behavior due indentation problem not visible in code above nor in text editor. turned out lastly print
statement indented 3 spaces tab, makes appear align else
, in fact either follows pass
in else
if it's there, or follows continue
in first part of if
. confusing me great deal.
here's screenshot of code in text editor "show space/tabs" alternative turned on. reddish dots represent spaces , reddish >>
represents tab character:
you don't need it. ran next 2 scripts:
#test1.py import os source_dir = '.' ext = '.txt' dirname, subdirlist, filelist in os.walk(source_dir): if not any(os.path.splitext(filename)[1].lower() == ext filename in filelist): print ' skipping "{}"'.format(dirname) go on else: # why clause needed go on iteration of loop? print 'contains "{}"'.format(dirname) pass print 'processing "{}" has "{}" files'.format(dirname, ext)
and
#test2.py import os source_dir = '.' ext = '.txt' dirname, subdirlist, filelist in os.walk(source_dir): if not any(os.path.splitext(filename)[1].lower() == ext filename in filelist): print ' skipping "{}"'.format(dirname) go on #else: # why clause needed go on iteration of loop? # print 'contains "{}"'.format(dirname) # pass print 'processing "{}" has "{}" files'.format(dirname, ext)
i ran them as:
python test1.py > junk.log python test2.py > junk.log2
here's first couple lines of junk.log
:
test $ head junk.log processing "." has ".txt" files skipping "./new" skipping "./unum" processing "./unum/kiv-unum-409befe069ac" 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 presence of "processing" lines.
then diff
output:
diff junk.log junk.log2
with next 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 there no differences on "processing" lines.
python python-2.7 if-statement continue
No comments:
Post a Comment