【问题标题】:How to capture information from executable jar in Python?如何从 Python 中的可执行 jar 中捕获信息?
【发布时间】:2013-01-16 19:35:53
【问题描述】:

我有一个可以在屏幕上打印一些信息的可执行 jar。我想从输出中捕获一个特定的数字,即成本。例如,有一行“Cost = 10”,当我运行 jar 文件 N 次时,我想有一个成本列表。我想用 python 脚本自动化这个过程,但由于我是 Python 新手,我不知道在 Python 脚本中从 stdout 中提取一些数据的最佳方法。完成此类任务的最佳方法是什么?

def wrapper(*args):
    process = Popen(list(args), stdout=subprocess.PIPE)
    process.communicate()
    return process

linkFileList = [ join(linkDir,f) for f in listdir(linkDir) if isfile(join(linkDir,f)) ]
nodeFileList = [ join(nodeDir,f) for f in listdir(nodeDir) if isfile(join(nodeDir,f)) ]
pairFileList = [ join(pairDir,f) for f in listdir(pairDir) if isfile(join(pairDir,f)) ]
size = len(linkFileList)
for count in range(0, size):
    x = wrapper('java', '-jar', 'C:\\Users\\fatma\\Documents\\workspace\\HeuristicRunnable.jar', nodeFileList[count], linkFileList[count], pairFileList[count])

【问题讨论】:

标签: java python


【解决方案1】:

你已经接近了。你想这样做:

def wrapper(*args):
    process = Popen(list(args), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return stdout, stderr

变量stdoutstderr 将是对应于jar 中stdout 和stderr 输出的简单字符串。我真的不认为你可以对 process 变量做任何事情,一旦你使用了它,所以返回它真的没有多大意义。

(我还包含了获取 stderr 所需的代码,以防万一也证明有用)

【讨论】:

  • 感谢您的回答,如何从函数返回的标准输出中提取信息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-07
  • 2013-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多