Skip to content

Commit d590b04

Browse files
FEAT: Add deletion statistics tracking to main.py
1 parent fd83dcd commit d590b04

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Folder Size Tagger/main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class BackgroundColors: # Colors for the terminal
8989
"Play Sound": True, # Set to True to play a sound when the program finishes
9090
}
9191

92+
# Deletion statistics accumulator:
93+
DELETION_STATS = {"files_deleted": 0, "dirs_deleted": 0, "bytes_deleted": 0} # Tracks deleted files, directories and total bytes
94+
9295
# Functions Definitions:
9396

9497

@@ -205,6 +208,21 @@ def convert_bytes_to_gb(size_bytes: int) -> float:
205208
return rounded_size_gb # Return rounded gigabyte value
206209

207210

211+
def update_deletion_stats(deleted_files: int, deleted_bytes: int) -> dict:
212+
"""
213+
Aggregate deletion statistics increments.
214+
215+
:param deleted_files: Number of files deleted in this operation.
216+
:param deleted_bytes: Total bytes deleted in this operation.
217+
:return: Current deletion statistics dictionary.
218+
"""
219+
220+
global DELETION_STATS # Reference global deletion statistics accumulator
221+
DELETION_STATS["files_deleted"] += int(deleted_files) # Increment files deleted counter
222+
DELETION_STATS["bytes_deleted"] += int(deleted_bytes) # Increment bytes deleted accumulator
223+
return DELETION_STATS # Return the updated statistics dictionary
224+
225+
208226
def format_directory_name(dirname: str, size_gb: float) -> str:
209227
"""
210228
Build the target directory name using the GB suffix format.
@@ -268,7 +286,13 @@ def delete_foto_directories(path: str) -> None:
268286

269287
try: # Protect recursive directory deletion
270288
import shutil # Import here for deletion operation
289+
files_count = 0 # Initialize file counter for this directory
290+
for _root, _dirs, files in os.walk(entry_path): # Traverse directory to count files
291+
files_count += len(files) # Increment file counter for each file found
292+
dir_size = calculate_directory_size_bytes(entry_path) # Compute directory size before deletion
271293
shutil.rmtree(entry_path) # Delete directory and all nested contents
294+
update_deletion_stats(files_count, dir_size) # Aggregate deleted files and bytes metrics
295+
DELETION_STATS["dirs_deleted"] += 1 # Increment deleted directories counter
272296
except (PermissionError, OSError): # Handle deletion access and OS failures
273297
continue # Skip failed deletion and continue processing
274298

@@ -329,6 +353,7 @@ def move_video_contents_to_parent(path: str) -> None:
329353

330354
try: # Protect source directory removal after content move
331355
os.rmdir(source_directory_path) # Remove now-empty Video target directory
356+
DELETION_STATS["dirs_deleted"] += 1 # Increment deleted directories counter after successful removal
332357
except (PermissionError, OSError): # Handle non-empty or inaccessible directory removal
333358
continue # Skip failed directory removal and continue processing
334359

@@ -429,9 +454,16 @@ def delete_empty_directories(paths: list) -> list:
429454
try: # Protect removal operation for empty directory
430455
if not os.listdir(p): # Verify if directory truly has no entries
431456
os.rmdir(p) # Remove empty directory
457+
DELETION_STATS["dirs_deleted"] += 1 # Increment deleted directories counter for empty dir
432458
else: # Handle case with no size but entries (defensive)
433459
import shutil # Import here for recursive removal
460+
files_count = 0 # Initialize file counter for recursive removal
461+
for _root, _dirs, files in os.walk(p): # Traverse directory tree to count files
462+
files_count += len(files) # Increment file counter for each file
463+
dir_size = calculate_directory_size_bytes(p) # Compute total size before recursive removal
434464
shutil.rmtree(p) # Remove directory recursively
465+
update_deletion_stats(files_count, dir_size) # Aggregate deleted files and bytes metrics for recursive removal
466+
DELETION_STATS["dirs_deleted"] += 1 # Increment deleted directories counter for recursive removal
435467
except (PermissionError, OSError): # Handle deletion access failures
436468
remaining.append(p) # Keep directory when deletion fails
437469
else: # Keep non-empty directories
@@ -633,6 +665,10 @@ def main():
633665
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}"
634666
) # Output the start and finish times
635667

668+
print(
669+
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}"
670+
) # Output aggregated deletion statistics
671+
636672
print(
637673
f"{BackgroundColors.BOLD}{BackgroundColors.GREEN}Program finished.{Style.RESET_ALL}"
638674
) # Output the end of the program message

0 commit comments

Comments
 (0)