@@ -93,6 +93,9 @@ class BackgroundColors: # Colors for the terminal
9393 "Play Sound" : True , # Set to True to play a sound when the program finishes
9494}
9595
96+ # Deletion statistics accumulator:
97+ DELETION_STATS = {"files_deleted" : 0 , "dirs_deleted" : 0 , "bytes_deleted" : 0 } # Tracks deleted files, directories and total bytes
98+
9699# Functions Definitions:
97100
98101
@@ -195,7 +198,13 @@ def delete_listed_directories(path: str) -> None:
195198 continue # Skip non-target directories
196199
197200 try : # Protect recursive directory deletion
201+ files_count = 0 # Initialize file counter for this directory
202+ for _root , _dirs , files in os .walk (entry_path ): # Traverse directory to count files
203+ files_count += len (files ) # Increment file counter for each file found
204+ dir_size = calculate_directory_size_bytes (entry_path ) # Compute directory size before deletion
198205 shutil .rmtree (entry_path ) # Delete directory and all nested contents
206+ update_deletion_stats (files_count , dir_size ) # Aggregate deleted files and bytes metrics
207+ DELETION_STATS ["dirs_deleted" ] += 1 # Increment deleted directories counter
199208 except (PermissionError , OSError ): # Handle deletion access and OS failures
200209 continue # Skip failed deletion and continue processing
201210
@@ -255,6 +264,7 @@ def move_video_contents_to_parent(path: str) -> None:
255264
256265 try : # Protect source directory removal after content move
257266 os .rmdir (source_directory_path ) # Remove now-empty Video target directory
267+ DELETION_STATS ["dirs_deleted" ] += 1 # Increment deleted directories counter after successful removal
258268 except (PermissionError , OSError ): # Handle non-empty or inaccessible directory removal
259269 continue # Skip failed directory removal and continue processing
260270
@@ -282,7 +292,9 @@ def delete_image_files_in_directory(path: str) -> None:
282292 if ext .lower () not in image_exts : # Verify extension is not an image type.
283293 continue # Skip non-image files.
284294 try : # Protect file deletion operation
295+ size_bytes = os .path .getsize (file_path ) # Get file size before removal
285296 os .remove (file_path ) # Remove the image file.
297+ update_deletion_stats (1 , size_bytes ) # Aggregate deletion stats for the removed file
286298 except (PermissionError , OSError ): # Handle deletion access failures
287299 continue # Skip files that cannot be removed.
288300
@@ -326,6 +338,21 @@ def convert_bytes_to_gb(size_bytes: int) -> float:
326338 return rounded_size_gb # Return rounded gigabyte value
327339
328340
341+ def update_deletion_stats (deleted_files : int , deleted_bytes : int ) -> dict :
342+ """
343+ Aggregate deletion statistics increments.
344+
345+ :param deleted_files: Number of files deleted in this operation.
346+ :param deleted_bytes: Total bytes deleted in this operation.
347+ :return: Current deletion statistics dictionary.
348+ """
349+
350+ global DELETION_STATS # Reference global deletion statistics accumulator
351+ DELETION_STATS ["files_deleted" ] += int (deleted_files ) # Increment files deleted counter
352+ DELETION_STATS ["bytes_deleted" ] += int (deleted_bytes ) # Increment bytes deleted accumulator
353+ return DELETION_STATS # Return the updated statistics dictionary
354+
355+
329356def format_directory_name (dirname : str , size_gb : float ) -> str :
330357 """
331358 Build the target directory name using the GB suffix format.
@@ -587,8 +614,15 @@ def delete_empty_directories(paths: list) -> list:
587614 try : # Protect removal operation for empty directory
588615 if not os .listdir (p ): # Verify if directory truly has no entries
589616 os .rmdir (p ) # Remove empty directory
617+ DELETION_STATS ["dirs_deleted" ] += 1 # Increment deleted directories counter for empty dir
590618 else : # Handle case with no size but entries (defensive)
619+ files_count = 0 # Initialize file counter for recursive removal
620+ for _root , _dirs , files in os .walk (p ): # Traverse directory tree to count files
621+ files_count += len (files ) # Increment file counter for each file
622+ dir_size = calculate_directory_size_bytes (p ) # Compute total size before recursive removal
591623 shutil .rmtree (p ) # Remove directory recursively
624+ update_deletion_stats (files_count , dir_size ) # Aggregate deleted files and bytes metrics for recursive removal
625+ DELETION_STATS ["dirs_deleted" ] += 1 # Increment deleted directories counter for recursive removal
592626 except (PermissionError , OSError ): # Handle deletion access failures
593627 remaining .append (p ) # Keep directory when deletion fails
594628 else : # Keep non-empty directories
@@ -804,6 +838,10 @@ def main():
804838 f"{ BackgroundColors .GREEN } Start time: { BackgroundColors .CYAN } { start_time .strftime ('%d/%m/%Y - %H:%M:%S' )} \n { BackgroundColors .GREEN } Finish time: { BackgroundColors .CYAN } { finish_time .strftime ('%d/%m/%Y - %H:%M:%S' )} \n { BackgroundColors .GREEN } Execution time: { BackgroundColors .CYAN } { calculate_execution_time (start_time , finish_time )} { Style .RESET_ALL } "
805839 ) # Output the start and finish times
806840
841+ print (
842+ f"{ BackgroundColors .GREEN } Total files deleted: { BackgroundColors .CYAN } { DELETION_STATS ['files_deleted' ]} { BackgroundColors .GREEN } | Total dirs deleted: { BackgroundColors .CYAN } { DELETION_STATS ['dirs_deleted' ]} { BackgroundColors .GREEN } | Total deleted size: { BackgroundColors .CYAN } { convert_bytes_to_gb (DELETION_STATS ['bytes_deleted' ]):.2f} GB{ Style .RESET_ALL } "
843+ ) # Output aggregated deletion statistics
844+
807845 print (
808846 f"{ BackgroundColors .BOLD } { BackgroundColors .GREEN } Program finished.{ Style .RESET_ALL } "
809847 ) # Output the end of the program message
0 commit comments