I use python script to compile a maven project.
If I use
from subprocess import call
call("mvn clean && mvn compile")
I can't define the directory of my maven project and maven tries to compile in directory of Python script.
Is there any standard way to define execution directory for the shell command?
your command should use
shell=True
since it's using 2 commands chained by an exit condition testcwd=your_directory
like this:
from subprocess import call
status = call("mvn clean && mvn compile",cwd="/users/foo/xxx",shell=True)
Personally I tend to avoid depending on shell=True
. In those cases I tend to decompose both commands and put arguments directly as argument list so if some nasty whitespace slips in it is quoted automatically:
if call(["mvn","clean"],cwd="/users/foo/xxx") == 0:
status = call(["mvn","compile"],cwd="/users/foo/xxx")
Note that if you want to get hold of the commands output from the python side (without redirecting to a file, which is dirty), you'll need Popen
with stdout=PIPE
instead of call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With