You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
5.5KB

  1. import shutil
  2. import time
  3. from glob import glob
  4. from os import remove, makedirs
  5. from os.path import join, isfile, exists, basename, splitext
  6. from traceback import format_exc
  7. from loguru import logger
  8. from enums.ExceptionEnum import ExceptionType
  9. from exception.CustomerException import ServiceException
  10. from util.ImageUtil import is_jpeg_image
  11. from util.VideoUtil import is_mp4_video
  12. def get_all_images(directory):
  13. images = []
  14. # 使用glob模块的通配符匹配查找所有图片文件
  15. image_files = glob(join(directory, '*.jpg')) + glob(join(directory, '*.jpeg')) + glob(join(directory, '*.png'))
  16. logger.info("识别到图片的数量: {}!", len(image_files))
  17. for image_file in image_files:
  18. if isfile(image_file) and is_jpeg_image(image_file):
  19. images.append(image_file)
  20. logger.info("识别是图片类型的数量: {}!", len(images))
  21. return images
  22. def get_all_videos(directory):
  23. videos = []
  24. # 使用glob模块的通配符匹配查找所有图片文件
  25. video_files = glob(join(directory, '*.mp4'))
  26. logger.info("识别到视频的数量: {}!", len(video_files))
  27. for video_file in video_files:
  28. if isfile(video_file) and is_mp4_video(video_file):
  29. videos.append(video_file)
  30. logger.info("识别是视频类型的数量: {}!", len(videos))
  31. return videos
  32. def delete_file(file_path, is_ex=False):
  33. retry_count = 0
  34. while retry_count < 3:
  35. try:
  36. if exists(file_path):
  37. remove(file_path)
  38. logger.info("删除文件成功: {}!", file_path)
  39. break
  40. except Exception:
  41. logger.error("删除文件失败: {}, 异常信息: {}", file_path, format_exc())
  42. if is_ex:
  43. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  44. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  45. finally:
  46. time.sleep(0.5)
  47. retry_count += 1
  48. def move_file(source_path, destination_path, is_ex=False):
  49. retry_count = 0
  50. while retry_count < 3:
  51. try:
  52. if exists(source_path) and exists(destination_path):
  53. shutil.move(source_path, destination_path)
  54. logger.info("迁移文件夹到指定文件夹成功! 原文件: {}, 目标文件: {}", source_path, destination_path)
  55. break
  56. except shutil.Error as sh:
  57. if "already exists" in str(sh):
  58. remove(source_path)
  59. break
  60. except OSError:
  61. logger.error("迁移文件夹到指定文件夹成功! 原文件: {}, 目标文件: {}, 异常信息: {}", source_path,
  62. destination_path,
  63. format_exc())
  64. if is_ex:
  65. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  66. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  67. finally:
  68. time.sleep(0.5)
  69. retry_count += 1
  70. def create_dir(path, is_ex=False):
  71. try:
  72. if not exists(path):
  73. makedirs(path)
  74. logger.info("创建文件{}成功!!!", path)
  75. except Exception:
  76. logger.info("创建文件{}失败!异常: {}", path, format_exc())
  77. if is_ex:
  78. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  79. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  80. def remove_file(path, is_ex=False):
  81. try:
  82. if exists(path):
  83. shutil.rmtree(path)
  84. except Exception:
  85. logger.info("删除{}文件异常, 异常: {}", path, format_exc())
  86. if is_ex:
  87. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  88. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  89. def remove_after_create(path, is_ex=False):
  90. try:
  91. if exists(path):
  92. shutil.rmtree(path)
  93. create_dir(path)
  94. except Exception:
  95. logger.info("删除{}文件夹异常, 异常: {}", path, format_exc())
  96. if is_ex:
  97. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  98. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  99. def get_file_name(path):
  100. try:
  101. return basename(path)
  102. except Exception:
  103. logger.info("获取{}文件名失败!异常: {}", path, format_exc())
  104. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  105. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  106. def get_file_name_0(path):
  107. try:
  108. return splitext(basename(path))[0]
  109. except Exception:
  110. logger.info("获取{}文件名失败!异常: {}", path, format_exc())
  111. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  112. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  113. def get_file_name_1(path):
  114. try:
  115. return splitext(basename(path))[1]
  116. except Exception:
  117. logger.info("获取{}文件名失败!异常: {}", path, format_exc())
  118. raise ServiceException(ExceptionType.SERVICE_INNER_EXCEPTION.value[0],
  119. ExceptionType.SERVICE_INNER_EXCEPTION.value[1])
  120. # if __name__ == '__main__':
  121. # # print(get_all_videos(r"D:\test\video"))
  122. # # move_file(r"D:\test\video\111.mp4", r'D:\test\video\aa')
  123. # # iamges = get_all_images(r"D:\test\image")
  124. # # for i in iamges:
  125. # # print(get_file_name_0(i))
  126. # remove_file(r"D:\tuoheng\codenew\tuoheng_airprt_media\tmp\oss\aaa")