files.py 865 B

12345678910111213141516171819202122232425262728293031323334
  1. from __future__ import print_function
  2. import os
  3. import glob
  4. def sortAscAsInt (aList):
  5. return sorted([f for f in aList if filterNames(f)], key=int)
  6. def filterNames (name):
  7. try:
  8. int(name)
  9. return True
  10. except:
  11. return False
  12. def filesFromFolder (path, foldername):
  13. try:
  14. fullPath = os.path.join(path, foldername)
  15. contentList = os.listdir(fullPath)
  16. return [s for s in sortAscAsInt(contentList) if os.path.isdir(os.path.join(fullPath, s))]
  17. except:
  18. print("Directory %s not found at %s." % (foldername, path))
  19. return []
  20. def countFolders (path, foldername = ""):
  21. files = filesFromFolder(path, foldername)
  22. return len(files)
  23. def highestFileName (path, foldername = ""):
  24. files = filesFromFolder(path, foldername)
  25. return max(files, key=int)
  26. def getFilesInFolder (folder, pattern):
  27. return glob.glob(folder + "/" + pattern)