os_tools.py 807 B

123456789101112131415161718192021222324252627282930
  1. import os
  2. from typing import List, Callable
  3. # For a given list of files and or directories, get all the
  4. def get_files(paths: List[str], filter: Callable[[str], bool]) -> List[str]:
  5. already_have = set()
  6. src_files = []
  7. def add_file(path):
  8. if path not in already_have:
  9. already_have.add(path)
  10. src_files.append(path)
  11. for p in paths:
  12. if os.path.isdir(p):
  13. # recursively scan directories
  14. for r, dirs, files in os.walk(p):
  15. files.sort()
  16. for f in files:
  17. if filter(f):
  18. add_file(os.path.join(r,f))
  19. elif os.path.isfile(p):
  20. add_file(p)
  21. else:
  22. print("%s: not a file or a directory, skipped." % p)
  23. return src_files
  24. xml_filter = lambda x: x.endswith('.xml')