Slightly modified from here, thanks Robin Parmar!
#!/usr/bin/python
def Walk( root, recurse=0, pattern='*', return_folders=0 ):
import fnmatch, os, string
# initialize
result = []
# must have at least root folder
try:
names = os.listdir(root)
except os.error:
return result
# expand pattern
pattern = pattern or '*'
pat_list = string.splitfields( pattern , ';' )
# check each file
for name in names:
fullname = os.path.normpath(os.path.join(root, name))
# grab if it matches our pattern and entry type
for pat in pat_list:
if fnmatch.fnmatch(string.lower(name), string.lower(pat)):
if os.path.isfile(fullname) or (return_folders and os.path.isdir(fullname)):
result.append(fullname)
continue
# recursively scan other folders, appending results. I have changed this
# so case does not matter
if recurse:
if os.path.isdir(fullname) and not os.path.islink(fullname):
result = result + Walk( fullname, recurse, pattern,
return_folders )
return result
# find all image files
if __name__ == '__main__':
# test code
print '\nExample 1:'
files = Walk('/home/httpd/twiki/pub/Test', 1, '*.jpg;*.gif;*.jpeg;*.png;*.bmp', 0)
for file in files:
print file
print 'There are %s files below current location:' % len(files)
--
MattWalsh - 03 Sep 2003