file_list.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. # lists for storing files and folders as FileObjects
  39. files = []
  40. dirs = []
  41. # inserts all file and folder names (strings) found in the path into file_names
  42. file_names = os.listdir(str(path))
  43. dir_names = []
  44. # removes some unwanted folders
  45. try:
  46. file_names.remove("System Volume Information")
  47. file_names.remove("$RECYCLE.BIN")
  48. file_names.remove("found.000")
  49. except:
  50. pass
  51. # find all folders from file_names and copy them to folder_names
  52. for f in file_names:
  53. if os.path.isdir(str(path) + "/" + f):
  54. dir_names.append(f)
  55. # removes folders from file_names and creates a FileObject for each folder
  56. for dir in dir_names:
  57. file_names.remove(dir)
  58. dir_obj = FileObject(dir, 0, "Folder")
  59. dirs.append(dir_obj)
  60. # creates a FileObject for each file
  61. for f in file_names:
  62. size = os.stat(str(path) + "/" + f).st_size
  63. type = determine_type(f)
  64. file_obj = FileObject(f, size, type)
  65. files.append(file_obj)
  66. return files, dirs