فهرست منبع

remove empty dirs

Murad Jabir 1 سال پیش
والد
کامیت
c7a8bb8e0f
1فایلهای تغییر یافته به همراه12 افزوده شده و 5 حذف شده
  1. 12 5
      main.py

+ 12 - 5
main.py

@@ -146,19 +146,26 @@ def create_nfo_file(video_data, file_path):
     with open(file_path, "w", encoding="utf-8") as nfo_file:
         nfo_file.write(nfo_content)
 
-def delete_old_videos(folder, days=10):
-    """Delete video files and corresponding NFO files older than `days`."""
+def delete_old_videos(folder, days=5):
+    """Delete video files and their folders older than `days`."""
     now = time.time()
     cutoff = now - (days * 86400)  # Convert days to seconds
 
-    for root, dirs, files in os.walk(folder):
+    for root, dirs, files in os.walk(folder, topdown=False):
+        # Delete files
         for file in files:
             file_path = os.path.join(root, file)
             if os.path.isfile(file_path):
-                # Check the file's last modification time
                 if os.path.getmtime(file_path) < cutoff:
-                    print(f"Deleting: {file_path}")
+                    print(f"Deleting file: {file_path}")
                     os.remove(file_path)
+        
+        # Remove empty directories
+        for dir in dirs:
+            dir_path = os.path.join(root, dir)
+            if not os.listdir(dir_path):  # Check if the directory is empty
+                print(f"Deleting folder: {dir_path}")
+                os.rmdir(dir_path)
 
 def video_exists(output_path, video_id):
     """Check if a video already exists in the directory."""