From 57e5794f777c659e9eda0df50f33ec5a2a9d61ce Mon Sep 17 00:00:00 2001 From: wsh5485 Date: Wed, 15 Jan 2025 18:04:36 +0800 Subject: [PATCH] 0.0.3 --- clean_logs.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/clean_logs.py b/clean_logs.py index 3066962..abe2ec0 100644 --- a/clean_logs.py +++ b/clean_logs.py @@ -35,9 +35,10 @@ def clean_log_files(directory): 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} ({file_size} bytes)\n") - print(f"Deleted: {file_path} ({file_size} bytes)") + size_mb = round(file_size / (1024 * 1024), 2) # 转换为MB + with open(log_file, 'a', encoding='utf-8') as f: + f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Deleted: {file_path} ({size_mb} MB)\n") + print(f"Deleted: {file_path} ({size_mb} MB)") total_size += file_size deleted_count += 1 except Exception as e: @@ -45,8 +46,9 @@ def clean_log_files(directory): # 记录本次清理的总大小 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") + total_mb = round(total_size / (1024 * 1024), 2) # 转换为MB + with open(log_file, 'a', encoding='utf-8') as f: + f.write(f"{datetime.now().strftime('%Y-%m-%d %H:%M:%S')} - Total: Deleted {deleted_count} files, {total_mb} MB\n") if __name__ == "__main__": if os.path.isdir(TARGET_DIR):