zhangyong 9 months ago
parent
commit
1a270bc1c6
1 changed files with 15 additions and 12 deletions
  1. 15 12
      video_rewriting/video_processor.py

+ 15 - 12
video_rewriting/video_processor.py

@@ -3,6 +3,7 @@ import json
 import os
 import os
 import random
 import random
 import re
 import re
+import shutil
 import time
 import time
 from datetime import datetime
 from datetime import datetime
 
 
@@ -60,18 +61,20 @@ class VideoProcessor:
         """
         """
         删除指定目录下的所有文件和子目录
         删除指定目录下的所有文件和子目录
         """
         """
-        try:
-            path = config['PATHS']['VIDEO_PATH'] + mark + "/"
-            if os.path.exists(path) and os.path.isdir(path):
-                for root, dirs, files in os.walk(path):
-                    for file in files:
-                        file_path = os.path.join(root, file)
-                        os.remove(file_path)
-                    for dir in dirs:
-                        dir_path = os.path.join(root, dir)
-                        os.rmdir(dir_path)
-        except:
-            pass
+        path = config['PATHS']['VIDEO_PATH'] + mark + "/"
+        # 删除目录下的所有内容
+        if os.path.exists(path):
+            # 遍历目录下的所有文件和子目录
+            for filename in os.listdir(path):
+                file_path = os.path.join(path, filename)
+                try:
+                    if os.path.isfile(file_path) or os.path.islink(file_path):
+                        os.unlink(file_path)  # 删除文件或符号链接
+                    elif os.path.isdir(file_path):
+                        shutil.rmtree(file_path)  # 删除子目录及其所有内容
+                except Exception as e:
+                    print(f'Failed to delete {file_path}. Reason: {e}')
+
 
 
 
 
     @classmethod
     @classmethod