find.find

G

Gigs_

import fnmatch, os

def find(pattern, startdir=os.curdir):
matches = []
os.path.walk(startdir, findvisitor, (matches, pattern))
matches.sort()
return matches

def findvisitor((matches, pattern), thisdir, nameshere): #
for name in nameshere:
if fnmatch.fnmatch(name, pattern):
fullpath = os.path.join(thisdir, name)
matches.append(fullpath)

can someone explain why (matches, pattern) is doing in this two funct?

thanks
 
M

Marc 'BlackJack' Rintsch

import fnmatch, os

def find(pattern, startdir=os.curdir):
matches = []
os.path.walk(startdir, findvisitor, (matches, pattern))
matches.sort()
return matches

def findvisitor((matches, pattern), thisdir, nameshere): #
for name in nameshere:
if fnmatch.fnmatch(name, pattern):
fullpath = os.path.join(thisdir, name)
matches.append(fullpath)

can someone explain why (matches, pattern) is doing in this two funct?

It's the first argument to `findvisitor()` which is invoked for every
directory level by `os.path.walk()`. `findvisitor()` adds all file names
that match `pattern` to the `matches` list.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top