Export command line output to a file in Python -
here's code execute jar file in python:
import os os.system("java -jar xyz.jar")
i can see output on terminal, want store in file. how can that?
with subprocess.call can pipe outputs (stdout, stderr or both) directly file:
import subprocess subprocess.call("java -jar xyz.jar", shell=true, stdout=open('outfile.txt', 'wt'), stderr=subprocess.stdout)
note added shell=true
parameter, required if application requires shell-specific variables (such java located).
also note in above call, output streams handled follows:
- stderr stream piped stdout, and
- stdout piped outfile
more details on stream configurations available in subprocess manual page.
Comments
Post a Comment