file_list.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import os
  2. from .PathObject import PathObject
  3. from .FileObject import FileObject
  4. filetype = {
  5. "txt": "Text",
  6. "doc": "Text",
  7. "rtf": "Text",
  8. "odt": "Text",
  9. "mp3": "Music",
  10. "flac": "Music",
  11. "wav": "Music",
  12. "mp4": "Video",
  13. "flv": "Video",
  14. "mkv": "Video",
  15. "avi": "Video",
  16. "webm": "Video",
  17. "mov": "Video",
  18. "jpg": "Image",
  19. "jpeg": "Image",
  20. "png": "Image",
  21. "gif": "Image",
  22. "bmp": "Image",
  23. "tiff": "Image",
  24. "webp": "Image",
  25. "exe": "Executable"
  26. }
  27. # determines a file's type, based on its extension
  28. def determine_type(file_name):
  29. try:
  30. extension = str(file_name).rsplit('.', 1)[1]
  31. type = filetype.get(extension, "File")
  32. return type
  33. except:
  34. return "File"
  35. # creates two FileObject lists, containing all the files and
  36. # folders found in the path given as argument
  37. def get_files_dirs(path):
  38. dir_path = str(path)
  39. # lists for storing files and folders as FileObjects
  40. files = []
  41. dirs = []
  42. # inserts all file and folder names (strings) found in the path into file_names
  43. file_names = os.listdir(dir_path)
  44. for file in file_names:
  45. abs_path = os.path.join(dir_path, file)
  46. if os.path.isfile(abs_path):
  47. files.append(file)
  48. elif os.path.isdir(abs_path):
  49. dirs.append(file)
  50. return files, dirs