0.0.2
This commit is contained in:
+14
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user