$ jsonlint -cq /home/test/notokay1.json
The above command has exit value 1 and the below output
/home/notokay1.json: line 6, col 1, found: 'EOF' - expected: '}', ','.
How can i capture both in Python.
$ jsonlint -cq /home/test/notokay1.json
The above command has exit value 1 and the below output
/home/notokay1.json: line 6, col 1, found: 'EOF' - expected: '}', ','.
How can i capture both in Python.
Use subprocess.Popen:
import subprocess
p = subprocess.Popen('jsonlint -cq /home/test/notokay1.json'.split(),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
print "Standard Output:", out
print "Standard Error Output:", err
print "Return Code:", p.returncode
You can use the subprocess module, more specifically the check_output method.
Suppose that you have a file called test.bash with the following contents:
echo "Hi"
exit 1
To capture both the exit code and the output you could do something like this:
# test.py file
import subprocess
exitCode = 0
output = ""
try:
output = subprocess.check_output(["bash", "test.bash"]) # get only ouput
except subprocess.CalledProcessError as e:
# get output and exit code
exitCode = e.returncode
output = e.output
print(output, exitCode)
Which outputs:
bash-4.2$ python test.py
('Hi\n', 1)
You just need to adapt this to your problem.
I beleive this is what you're looking for:
$ jsonlint -cq /home/test/notokay1.json > stdout.txt; echo $? > stderr.txt
you can then use python's built in file I/O to read stdout.txt and stderr.txt
refrence: http://www.tldp.org/LDP/abs/html/io-redirection.html