String compare question

Tim Chase python.list at tim.thechases.com
Mon Feb 25 12:27:22 EST 2008


>> ignored_dirs = (
>>    r".\boost\include",  # It's that comma that makes this a tuple.
>>     )
>>
> 
> Thanks for reminding me of this. I always forget that!
> 
> Now that it is correctly doing *only* whole string matches, what if I want
> to make it do a substring compare to each string in my ignored_dirs tuple?

Sounds like a good opportunity for the underused for/break/else 
construct:

######################################################
ignored_dirs = (
     r".\boost\include",
     )

if __name__ == "__main__":
     # Walk the directory tree rooted at 'source'
     for root, dirs, files in os.walk( source ):
	for dirname in ignored_dirs:
             # may need to normalize to root.lower()
             if dirname in root:
                 print 'Skipping', root
                 break
         else:
             CopyFiles( root, files, ".dll" )
######################################################

-tkc





More information about the Python-list mailing list