This commit is contained in:
2025-01-15 18:04:36 +08:00
parent 035cb9d75a
commit 57e5794f77
+7 -5
View File
@@ -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):