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