From ac69e84f6af0a0d6f5bf1a6f7f32a1ffe873f5df Mon Sep 17 00:00:00 2001 From: chenxudong Date: Thu, 5 Jun 2025 10:49:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=88=A0=E9=99=A4=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=96=87=E4=BB=B6=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/start.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/start.py b/src/start.py index 62e0024..e2f03bb 100644 --- a/src/start.py +++ b/src/start.py @@ -34,6 +34,40 @@ def get_resource_path(relative_path): # 构建JDK和JAR的路径 jar_path = os.path.join("datas", "electromagnetic.jar") +def delete_old_files(directory, days=2): + # 计算时间阈值(当前时间 - days天) + threshold_time = time.time() - days * 24 * 60 * 60 + + deleted_count = 0 + try: + # 遍历目录中的文件 + for filename in os.listdir(directory): + filepath = os.path.join(directory, filename) + + # 确保是文件而不是目录 + if os.path.isfile(filepath): + try: + # 获取文件创建时间(Windows系统) + creation_time = os.path.getctime(filepath) + + # 检查文件是否超过阈值 + if creation_time < threshold_time: + # 删除文件 + os.remove(filepath) + deleted_count += 1 + logger.info(f"已删除: {filename} (创建时间: {format_time(creation_time)})") + except Exception as e: + logger.info(f"处理文件 {filename} 时出错: {str(e)}", file=sys.stderr) + + logger.info(f"\n操作完成!共删除 {deleted_count} 个文件。") + except Exception as e: + logger.info(f"遍历目录时出错: {str(e)}", file=sys.stderr) + + +def format_time(timestamp): + """将时间戳格式化为可读字符串""" + return datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S') + def start(): try: ensure_dir(app_log_dir) @@ -91,6 +125,7 @@ def open_web(): if __name__ == '__main__': elevate.elevate() kill_process_by_port(port) + delete_old_files(app_log_dir, days=2) start() time.sleep(10) open_web() \ No newline at end of file