From 035cb9d75a7c18d298eddaad3923a970c19aafb3 Mon Sep 17 00:00:00 2001 From: wsh5485 Date: Wed, 15 Jan 2025 18:01:43 +0800 Subject: [PATCH] 0.0.2 --- clean_logs.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/clean_logs.py b/clean_logs.py index 42a8edd..3066962 100644 --- a/clean_logs.py +++ b/clean_logs.py @@ -3,7 +3,7 @@ import time from datetime import datetime, timedelta # 用户可以在此处修改要清理的目录 -TARGET_DIR = "e:/path/to/your/logs" # 请将此路径修改为实际要清理的目录 +TARGET_DIR = "E:\pythontest\CLEAN_EVERYTHING\新建文件夹" # 请将此路径修改为实际要清理的目录 def clean_log_files(directory): # 获取脚本所在目录 @@ -23,20 +23,31 @@ def clean_log_files(directory): f.write(line) f.truncate() + total_size = 0 # 初始化总大小统计 + deleted_count = 0 # 初始化删除文件计数 + # 遍历目录并删除.log文件 for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.log'): file_path = os.path.join(root, file) try: + file_size = os.path.getsize(file_path) # 获取文件大小 os.remove(file_path) # 记录删除操作 with open(log_file, 'a') as f: - f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Deleted: {file_path}\n") - print(f"Deleted: {file_path}") + f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Deleted: {file_path} ({file_size} bytes)\n") + print(f"Deleted: {file_path} ({file_size} bytes)") + total_size += file_size + deleted_count += 1 except Exception as e: print(f"Error deleting {file_path}: {e}") + # 记录本次清理的总大小 + if deleted_count > 0: + with open(log_file, 'a') as f: + f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Total: Deleted {deleted_count} files, {total_size} bytes\n") + if __name__ == "__main__": if os.path.isdir(TARGET_DIR): clean_log_files(TARGET_DIR)