Using filter()

Larry Whitley ldw at us.ibm.com
Tue Jul 18 10:06:41 EDT 2000


I have a simple problem and from the description, filter() seems like a
match.  But I can't figure out how to make it work.  Here's the code without
using filter()

include string, os

def filter1( fileList, name ):
#--------------------------------------------------------
# matches name to the first characters of a file in the file list
 temp = []
 for i in range(len(fileList)):
    if string.find( fileList[i], name ) == 0:
        temp.append( fileList[i] )
 return temp

def filter2( filelist, aString ):
#-------------------------------------------------
# finds aString somewhere in the file list
 temp = []
 for i in range(len(filelist)):
    if string.find( filelist[i], aString ) != -1: # is in the string
somewhere
        temp.append( filelist[i] )
 return temp

def main(): #--------------------------------------------------------------
    # somewhere in main
    tracePath = 'u:\\trc45\\junebug\\'
    traceFileName = 'jbtpcc.'
    traceFileExt = '.gz'
    infiles = filter2( filter1( os.listdir( tracePath ), traceFileName ),
traceFileExt )
    infiles.sort()
    print infiles

I suspect using filter() would simplify this.  Can someone help?

Larry





More information about the Python-list mailing list