[Tutor] Preffered way to search posix filesystem

Jacob S. keridee at jayco.net
Fri Jan 28 03:10:15 CET 2005


Try this

def findfile(thefile, 
toplevel="C:\\windows",findcount=-1,subdirectories=True,returnlist=False):
    results = []
    t = 0
    if subdirectories:
        for root,dirs,files in os.walk(toplevel):
            for x in files:
                fullname = os.path.join(root,x)
                if x.lower() == thefile.lower():
                    print "Found %s" % fullname
                    results.append(fullname+": 0:")
                    t = t+1
                else:
                    print "Searching %s" % fullname
            if t == findcount:
                break
        print "\nFound %s matches:" % t
        pplist(results)
    else:
        n = 0
        for x in os.listdir(toplevel):
            fullname = os.path.join(toplevel,x)
            if x.lower()==thefile.lower():
                print "Found:\n%s: 0:" % fullname
                n = 1
                results.append(fullname)
                break
        if not n:
            print "Found 0 matches."
    if returnlist:
        return results



More information about the Tutor mailing list