check output python

Solutions on MaxInterview for check output python by the best coders in the world

showing results for - "check output python"
Axel
06 Apr 2019
1import subprocess
2py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
3print('py2 said:', py2output)
4
Stefano
10 Mar 2018
1The right answer (using Python 2.7 and later, since check_output() was introduced then) is:
2
3py2output = subprocess.check_output(['python','py2.py','-i', 'test.txt'])
4To demonstrate, here are my two programs:
5
6py2.py:
7
8import sys
9print sys.argv
10py3.py:
11
12import subprocess
13py2output = subprocess.check_output(['python', 'py2.py', '-i', 'test.txt'])
14print('py2 said:', py2output)
15Running it:
16
17$ python3 py3.py
18py2 said: b"['py2.py', '-i', 'test.txt']\n"
19Here's what's wrong with each of your versions:
20
21py2output = subprocess.check_output([str('python py2.py '),'-i', 'test.txt'])
22First, str('python py2.py') is exactly the same thing as 'python py2.py'—you're taking a str, and calling str to convert it to an str. This makes the code harder to read, longer, and even slower, without adding any benefit.
23
24More seriously, python py2.py can't be a single argument, unless you're actually trying to run a program named, say, /usr/bin/python\ py2.py. Which you're not; you're trying to run, say, /usr/bin/python with first argument py2.py. So, you need to make them separate elements in the list.
25
26Your second version fixes that, but you're missing the ' before test.txt'. This should give you a SyntaxError, probably saying EOL while scanning string literal.
27
28Meanwhile, I'm not sure how you found documentation but couldn't find any examples with arguments. The very first example is:
29
30>>> subprocess.check_output(["echo", "Hello World!"])
31b'Hello World!\n'
32That calls the "echo" command with an additional argument, "Hello World!".
33
34Also:
35
36-i is a positional argument for argparse, test.txt is what the -i is
37
38I'm pretty sure -i is not a positional argument, but an optional argument. Otherwise, the second half of the sentence makes no sense.
queries leading to this page
python3 subprocess check outputsubprocess check output pythoncheck output in pythonsubprocess check output example subprocess check outputcheck output printsubprocess check output pythonsubprocess check outputpython3 subprocess check outputpython subprocess check output 28python check output functionsubprocess check output return codepython subprocess to get outputsubprocess check output in python subprocess check output errorsubprocess check output python examplecheck output subprocesspython subprocess check outputsubprocess check output python 3check output 28 29 in pythonpython subprocess get outputpython check outputsubprocess get output with check outputerror in check output function inpythonwhat is check output subprocesssubprocess check output examplewhat is subprocess check outputread subprocess check call output pythonpython subprocess get output as stringsubprocess check output requestsubprocess check output 2fcheck output subprocess python 3subprocess check outputsubprocess check output 28subprocess check outputpython3 check outputpython subprocess check outputsubprocess python check outputpython os subprocess check outputcheck output python examplepython subprocess check output examplecheck output pythonsubprocess check output b 22display subprocess check outputsubprocess check output python examplepython subprocess check outputcheck output pythonfrom subprocess import check outputsubprocess check output 28 29subprocess check output interpretercheck output python