You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何从CAPL脚本传递输入参数至Python脚本并执行

解决方案:从CAPL向Python脚本传递搜索关键词实现复用

问题背景

现有一款Python脚本可在DLT日志中搜索关键词,运行正常,但每次搜索不同关键词时需修改脚本内容。由于正在开展测试用例自动化工作,无法通过弹窗输入关键词,需要实现从CAPL函数中指定搜索关键词,让Python脚本无需修改即可复用。

修改后的Python代码

import os
import glob
import sys

def get_latest_dlt_file(folder_path):
    # 获取文件夹中所有.dlt文件
    dlt_files = glob.glob(os.path.join(folder_path, "*.dlt"))
    # 过滤目录,获取修改时间并按修改时间倒序排序
    dlt_files = [(f, os.path.getmtime(f)) for f in dlt_files if os.path.isfile(f)]
    dlt_files.sort(key=lambda x: x[1], reverse=True)
    # 返回最新修改的.dlt文件路径(如果存在)
    return dlt_files[0][0] if dlt_files else None

def dlt_to_txt_converter(dlt_file_path, txt_file_path):
    try:
        with open(dlt_file_path, 'r', encoding='utf-8', errors='ignore') as dlt_file:
            dlt_content = dlt_file.read()

        with open(txt_file_path, 'w', encoding='utf-8') as txt_file:
            txt_file.write(dlt_content)

        print(f"转换成功!内容已写入 {txt_file_path}")
    except Exception as e:
        print(f"转换发生错误: {e}")

def search_string_line_by_line(txt_file, search_string):
    found = False
    try:
        try:
            with open(txt_file, 'r', encoding='utf-8', errors='ignore') as file:
                print(f"以utf-8编码打开文件: {txt_file}")
                for line in file:
                    if search_string in line:
                        found = True
                        break
        except UnicodeDecodeError:
            with open(txt_file, 'r', encoding='latin1', errors='ignore') as file:
                print(f"以latin1编码打开文件: {txt_file}")
                for line in file:
                    if search_string in line:
                        found = True
                        break

        with open("DLT_Message.txt", "w", encoding='utf-8') as message_found:
            if found:
                message_found.write("1")
                print("在文本文件中找到目标字符串。")
            else:
                message_found.write("0")
                print("未在文本文件中找到目标字符串。")
    except FileNotFoundError:
        print(f"文件未找到: {txt_file}")
    except Exception as e:
        print(f"搜索发生错误: {e}")

if __name__ == "__main__":
    # 检查命令行参数是否足够
    if len(sys.argv) < 2:
        print("错误:请传入搜索关键词作为命令行参数")
        os._exit(1)
    
    # 从命令行参数获取搜索关键词
    search_string = sys.argv[1]
    # 指定存放.dlt文件的文件夹
    folder_path = r"C:\Software\Python"

    # 获取最新修改的.dlt文件路径
    latest_dlt_file = get_latest_dlt_file(folder_path)

    if latest_dlt_file:
        # 定义转换后的.txt文件路径
        txt_file_path = os.path.join(os.path.dirname(latest_dlt_file), os.path.splitext(os.path.basename(latest_dlt_file))[0] + ".txt")

        # 转换最新的.dlt文件为.txt
        dlt_to_txt_converter(latest_dlt_file, txt_file_path)

        # 在.txt文件中搜索关键词
        search_string_line_by_line(txt_file_path, search_string)
    else:
        print("指定文件夹中未找到.dlt文件。")
        
    # 退出程序
    os._exit(0)

修改后的CAPL代码

void Search_Logs(char searchKeyword[])
{
    char cmd[256];
    // 拼接命令行,传递搜索关键词作为参数
    snprintf(cmd, sizeof(cmd), "search_logs.py %s", searchKeyword);
    sysExecCmd(cmd, "", putty_pythonScriptPath);
}

关键改动说明

  • Python脚本:
    • 新增命令行参数检查逻辑,确保外部传入搜索关键词
    • 从sys.argv[1]获取外部传入的搜索关键词,替代原硬编码的固定值
    • 增加参数缺失时的错误提示,避免无意义执行
  • CAPL函数:
    • 修改函数为接收字符串类型的搜索关键词参数
    • 使用snprintf拼接完整命令行,将搜索关键词作为参数传递给Python脚本
    • 通过sysExecCmd执行带参数的Python脚本命令

内容的提问来源于stack exchange,提问作者Muhammad Faizan Bukhari

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:14:50