[Tutor] Filename match on file extensions

Dave Angel d at davea.name
Wed Oct 3 07:09:08 CEST 2012

On 10/02/2012 11:45 PM, Dave Wilder wrote:
>>> Hello,
>>> Below is a snippet of a search I want to do for any file that contains the string "quarantine" in the filename.
>>> It works for picking up any file name containing "quarantine" except when "quarantine" is used as an extension.
>>> For example, the search would find quarantine.txt but fails to find txt.quarantine.  I have done some investigating, but have
>>> Not been able to determine how to make it include extensions in the search.
>>> I believe I should be using something like "os.path.splitext".  Does that make sense? Thanks.
>>> target = '/home'
>>> matches = 0
>>> for (currdir,subdirs,files) in os.walk(target):
>>>     for i in files:
>>>     if i.lower().find("quarantine".lower()) == 0: ## Case-insensitive search for string "quarantine" in filename
>>>          matches += 1   # increment number of matches
>>>            print "Quarantine file",i,"found in directory",currdir
>>> if matches > 0:
>>>    result = tc.FAIL
>>> else:
>>>    result = 'FAIL'
>> Would this be a time when regex is necessary?   Maybe: \b[^.]*quarantine[^.]*\.[a-zA-Z]*\b
> I had originally used regular expression, but thought there might be a simpler solution w/o the need for regular expressions.
> If that is what is needed, then so be it though.  Thank you for your quick response.

A regex cannot possibly help.  Please don't make that mistake.

Have you actually displayed the names it claims do not contain the
quarantine string?  In other words, put an else clause in?  Your
problem/solution is elsewhere, not in splitext, and not regex.

Start by looking up the doc for the index method.  It returns -1 for
failure, not 0.  You're explicitly checking for a string STARTing with
'quarantine'. So it'll skip files like
as well as

>From your description, you should just be using 'in' operator.

if "quarantine" in i.lower():


More information about the Tutor mailing list