find.py 757 B

1234567891011121314151617181920212223242526
  1. import fnmatch
  2. import os
  3. _debug = 0
  4. _prune = ['(*)']
  5. def find(pattern, dir = os.curdir):
  6. list = []
  7. names = os.listdir(dir)
  8. names.sort()
  9. for name in names:
  10. if name in (os.curdir, os.pardir):
  11. continue
  12. fullname = os.path.join(dir, name)
  13. if fnmatch.fnmatch(name, pattern):
  14. list.append(fullname)
  15. if os.path.isdir(fullname) and not os.path.islink(fullname):
  16. for p in _prune:
  17. if fnmatch.fnmatch(name, p):
  18. if _debug: print "skip", `fullname`
  19. break
  20. else:
  21. if _debug: print "descend into", `fullname`
  22. list = list + find(pattern, fullname)
  23. return list