boldbrandywine boldbrandywine - 3 months ago 23
Scala Question

Executing sbt in command line via python script and outputting to file

I have a set of json files in directory

/Desktop/jsons
, and I have a Scala script which takes in a json and outputs stuff. I can run it manually in the terminal by
cd
ing into the directory of the Scala script (
/Me/dev/scalastuff
) and running

sbt --error "run /Desktop/jsons/jsonExample.json"
,

which outputs the stuff I want in the terminal.

I want to write a Python script which does this automatically and additionally outputs a json file with the "stuff" thats outputted by the Scala script.

My issues right now are using subprocessing. When I try to run

BASEDIR = '/Me/dev/scalastuff'
p = subprocess.Popen(['sbt --error "run /Desktop/jsons/jsonExample.json"'], cwd = BASEDIR, stdout = subprocess.PIPE)
out = p.stdout.read()
print out


I get
OSError: [Errno 2] No such file or directory
.

I'm completely stumped as to why this is occurring. I'm new to subprocess, so be light on me!

Ven Ven
Answer

popen in python takes a list of shell arguments. You're passing only one!

So it's trying to execute a file named wholly 'sbt --error "run /Me/Desktop/jsons/jsonExample.json"'.

Obviously, this doesn't work.

If you use popen; only pass a simple array -- you needn't care about escaping:

subprocess.popen(['sbt', '--error', 'run /Me/Desktop/...'], cwd = BASEDIR, stdout = subprocess.PIPE)
Comments