Jython从文件中获取结果系统出口()

2024-04-26 06:32:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从Java(Jython)的execfile()触发的Python脚本中获取结果。 这是正在工作的java端(对于不可见的静态导入,很抱歉):

    // prepare script stream:
    String scriptString = scriptProvider.getScriptFile().getRoot().toString();

    Path scriptPath = get(scriptString);
    checkState(exists(scriptPath), "Script file does not exist in %s", scriptString);

    InputStream scriptStream;
    try {
        scriptStream = newInputStream(scriptPath);
    } catch (IOException e) {
        throw new IllegalStateException("Exception occured while getting InputStream from scriptPath.", e);
    }

    // prepare interpreter and arguments:
    initialize();

    PySystemState state = new PySystemState();
    for (String file : checkFiles) {
        state.argv.append(new PyString(file));
    }

    PythonInterpreter interpreter = new PythonInterpreter(null, state);

    // execute script:
    interpreter.execfile(scriptStream);

请注意,我可能不会更改在那里执行的文件,我只想获取从控制台运行时正常打印的结果。我不习惯Python,但我认为主要的输出点是:

^{pr2}$

尤其是最后一行。 你知道如何从Java端获取这些信息吗?在


Tags: newstringscriptjavapreparefilestateinterpreter