How do you use subprocess.check_output() in Python?

I have found documentation about subprocess.check_output() but I cannot find one with arguments and the documentation is not very in depth. I am using Python 3 (but am trying to run a Python 2 file through Python 3)

I am trying to run this command:
python -i test.txt

-i is a positional argument for argparse, test.txt is what the -i is, is the file to run

I have tried a lot of (non working) variations including:
py2output = subprocess.check_output([str('python '),'-i', 'test.txt'])

py2output = subprocess.check_output([str('python'),'','-i', test.txt'])

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

The right answer (using Python 2.7 and later, since check_output() was introduced then) is:

py2output = subprocess.check_output(['python','','-i', 'test.txt'])

To demonstrate, here are my two programs:

import sys
print sys.argv

import subprocess
py2output = subprocess.check_output(['python', '', '-i', 'test.txt'])
print('py2 said:', py2output)

Running it:

$ python3
py2 said: b"['', '-i', 'test.txt']\n"

Here’s what’s wrong with each of your versions:

py2output = subprocess.check_output([str('python '),'-i', 'test.txt'])

First, str('python') is exactly the same thing as 'python'—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.

More seriously, python can’t be a single argument, unless you’re actually trying to run a program named, say, /usr/bin/python\ Which you’re not; you’re trying to run, say, /usr/bin/python with first argument So, you need to make them separate elements in the list.

Your 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.

Meanwhile, I’m not sure how you found documentation but couldn’t find any examples with arguments. The very first example is:

>>> subprocess.check_output(["echo", "Hello World!"])
b'Hello World!\n'

That calls the "echo" command with an additional argument, "Hello World!".


-i is a positional argument for argparse, test.txt is what the -i is

I’m pretty sure -i is not a positional argument, but an optional argument. Otherwise, the second half of the sentence makes no sense.

Solution 2

Since Python 3.5, is recommended instead of subprocess.check_output:

>>>['cat','/tmp/text.txt'], check=True, stdout=subprocess.PIPE).stdout
b'First line\nSecond line\n'

Since Python 3.7, instead of the above, you can use capture_output=true parameter to capture stdout and stderr:

>>>['cat','/tmp/text.txt'], check=True, capture_output=True).stdout
b'First line\nSecond line\n'

Also, you may want to use universal_newlines=True or its equivalent since Python 3.7 text=True to work with text instead of binary:

>>> stdout =['cat', '/tmp/text.txt'], check=True, capture_output=True, text=True).stdout
>>> print(stdout)
First line
Second line

Solution 3

Adding on to the one mentioned by @abarnert

a better one is to catch the exception

import subprocess
    py2output = subprocess.check_output(['python', '', '-i', 'test.txt'],stderr= subprocess.STDOUT)  
    #print('py2 said:', py2output)
    print "here"
except subprocess.CalledProcessError as e:
    print "Calledprocerr"

this stderr= subprocess.STDOUT is for making sure you dont get the filenotfound error in stderr- which cant be usually caught in filenotfoundexception, else you would end up getting

python: can't open file '': [Errno 2] No such file or directory

Infact a better solution to this might be to check, whether the file/scripts exist and then to run the file/script

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply