我正在尝试从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,但我认为主要的输出点是:
def main():
arguments = parse_command_line_arguments()
set_up_logging(arguments.loglevel)
result = check_files(arguments.files)
sys.exit(result)
特别是最后一行。 你有任何想法如何从Java端获取这个?