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

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.

Leave a Comment