How to execute an external program

I am trying to execute a java program from a python program:

subprocess.Popen(["java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar"], shell=True) 

but this error fails:

 Error: Unable to access jarfile /sphinx4-1.0beta5/bin/HelloWorld.jar 

I need to be in a specific directory: /home/karen/sphinx4-1.0beta-src to execute the command: "java -mx256m -jar /sphinx4-1.0beta5/bin/HelloWorld.jar" But I do not know how to do this. I need my python program to execute it!

+4
source share
2 answers

use cwd parameter

 subprocess.Popen(["java -mx256m -jar ../sphinx4-1.0beta5/bin/HelloWorld.jar"], cwd=r'path', shell=True) 

http://docs.python.org/2/library/subprocess.html "If cwd is not None, the current childs directory will be changed to cwd before it is executed. Please note that this directory is not considered when looking for an executable, so you you cannot specify the path to the program relative to cwd. "

+6
source

Your problem is probably related to your jar file path. Your code should most likely call / home / Karen / sphynx 4-1beta-src in a popup call. This is not a solution that will work on another system, unless the file is in the same absolute path.

0
source

All Articles