I have an executable jar which prints some information to the screen. I want to capture a specific number from the output which is the cost. There is a line “Cost = 10” for example and I want to have a list of costs when I run the jar file N times. I wanted to automate this process with a python script but since I am new to Python I do not know the best way to extract some data from stdout in a Python script. What would be the best way to do such task?
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])
You’re close. You want to do this instead:
The variables
stdoutandstderrwill be simple strings corresponding to the output from stdout and stderr from the jar. I don’t really think you can do anything with theprocessvariable once you’ve used it, so there really isn’t much point in returning it.(I also included the code you need to get the stderr in case that also proves to be useful)