Python subprocess check output not working
Question:
I’m trying to run my test_script.py in main_script.py with subprocess. test_script.py is a siple sum program, and main_script.py should call it with 2 arguments, and catch output. Here is the code:
test_script.py
a = int(input())
b = int(input())
print(a+b)
main_script.py
import subprocess
subprocess.check_output(['python', 'test_script.py', 2,3])
This is the error im getting:
Traceback (most recent call last):
File "C:/Users/John/Desktop/main_script.py", line 2, in <module>
subprocess.check_output(['python', 'test_script.py', 2,3])
File "C:Python34libsubprocess.py", line 607, in check_output
with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
File "C:Python34libsubprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:Python34libsubprocess.py", line 1085, in _execute_child
args = list2cmdline(args)
File "C:Python34libsubprocess.py", line 663, in list2cmdline
needquote = (" " in arg) or ("t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
Answers:
All parts of argument must a string. Do the following instead:
subprocess.check_output(['python', 'test_script.py', "2", "3"])
If this command fails to run, you’ll get an exception. To catch it and see the output:
try:
subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
Your second script will fail because it’s expecting input from the stdin, while your master script is sending the 2
and 3
as arguments. Investigate sys.argv
https://docs.python.org/3/library/subprocess.html#subprocess.check_output
import subprocess
subprocess.check_output("python test_script.py", stderr=subprocess.STDOUT, shell=True)
Actually the subprocess check output command only returns the value. So, you need a variable to store it in. So you can try this:
result = subprocess.check_output("python test_script.py",
stderr=subprocess.STDOUT, shell=True)
print(result)
I’m trying to run my test_script.py in main_script.py with subprocess. test_script.py is a siple sum program, and main_script.py should call it with 2 arguments, and catch output. Here is the code:
test_script.py
a = int(input())
b = int(input())
print(a+b)
main_script.py
import subprocess
subprocess.check_output(['python', 'test_script.py', 2,3])
This is the error im getting:
Traceback (most recent call last):
File "C:/Users/John/Desktop/main_script.py", line 2, in <module>
subprocess.check_output(['python', 'test_script.py', 2,3])
File "C:Python34libsubprocess.py", line 607, in check_output
with Popen(*popenargs, stdout=PIPE, **kwargs) as process:
File "C:Python34libsubprocess.py", line 858, in __init__
restore_signals, start_new_session)
File "C:Python34libsubprocess.py", line 1085, in _execute_child
args = list2cmdline(args)
File "C:Python34libsubprocess.py", line 663, in list2cmdline
needquote = (" " in arg) or ("t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
All parts of argument must a string. Do the following instead:
subprocess.check_output(['python', 'test_script.py', "2", "3"])
If this command fails to run, you’ll get an exception. To catch it and see the output:
try:
subprocess.check_output(['python', 'test_script.py', "2", "3"], stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print e.output
Your second script will fail because it’s expecting input from the stdin, while your master script is sending the 2
and 3
as arguments. Investigate sys.argv
https://docs.python.org/3/library/subprocess.html#subprocess.check_output
import subprocess
subprocess.check_output("python test_script.py", stderr=subprocess.STDOUT, shell=True)
Actually the subprocess check output command only returns the value. So, you need a variable to store it in. So you can try this:
result = subprocess.check_output("python test_script.py",
stderr=subprocess.STDOUT, shell=True)
print(result)