423

I'm trying to make a system call in Python and store the output to a string that I can manipulate in the Python program.

#!/usr/bin/python
import subprocess
p2 = subprocess.Popen("ntpq -p")

I've tried a few things including some of the suggestions here:

Retrieving the output of subprocess.call()

but without any luck.

Community
  • 1
  • 1
Mark
  • 7,306
  • 8
  • 45
  • 87
  • 3
    It is good always to post actual code you ran and the actual traceback or unexpected bahaviour for concrete questions like this. For example, I do not know what you tried to do to get the output and I suspect you didn't actually get that far to start with—you would have gotten an error about not finding the file for `"ntpq -p"`, which is a different part of the problem than you're asking about. – Mike Graham Mar 23 '10 at 19:39

15 Answers15

591

In Python 2.7 or Python 3

Instead of making a Popen object directly, you can use the subprocess.check_output() function to store output of a command in a string:

from subprocess import check_output
out = check_output(["ntpq", "-p"])

In Python 2.4-2.6

Use the communicate method.

import subprocess
p = subprocess.Popen(["ntpq", "-p"], stdout=subprocess.PIPE)
out, err = p.communicate()

out is what you want.

Important note about the other answers

Note how I passed in the command. The "ntpq -p" example brings up another matter. Since Popen does not invoke the shell, you would use a list of the command and options—["ntpq", "-p"].

Alois Mahdal
  • 10,763
  • 7
  • 51
  • 69
Mike Graham
  • 73,987
  • 14
  • 101
  • 130
  • 4
    In this case, does python wait for this system call to finish? Or is it necessary to explicitly call the wait/waitpid function? – NoneType Jul 22 '10 at 11:35
  • 10
    @NoneType, `Popen.communicate` does not return until the process has terminated. – Mike Graham Jul 23 '10 at 02:34
  • 12
    if you want to get error stream add stderr: `p = subprocess.Popen(["ntpq", "-p"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)` – Timofey May 27 '14 at 12:29
  • Giving +1 for `check_output()`, but I wish there was an answer that said to use it and didn't waste time bringing up `communicate()`, or brought up it up only as an alternative for older versions of Python at the end of the answer. – ArtOfWarfare Feb 10 '15 at 22:18
  • 1
    @ArtOfWarfare, I re-ordered the answers to try to be the most helpful to people today. Pre-2.7 Pythons are very much still in use, so it doesn't make much sense to completely omit how to do it with a `Popen` object (and it's useful anyhow to know). (Most of the answers don't use `check_output` because they predate Python 2.7.) – Mike Graham Feb 14 '15 at 20:09
  • @MikeGraham Is it possible to write/get the `out` in a particular directory.. ?! – diffracteD May 02 '15 at 20:42
  • @diffracteD, `with open('/path/to/save/output', 'w') as f:` `p = subprocess.Popen(["ntpq", "-p"], stdout=f)` `p.wait()` – Mike Graham May 03 '15 at 14:08
  • 1
    What If I want a pipe in my command like this: ps ax | grep foo ? – Master Bee Jul 19 '15 at 05:40
  • @BernhardEssl (1) Just use psutil, (2) if you're going to use ps in a subprocess, don't use grep to search it, just use plain old Python stuff, (3) `ps_process = Popen(['ps', 'ax'], stdout=PIPE); Popen(['grep', 'foo'], stdin=ps_process.stdout)` – Mike Graham Jul 19 '15 at 11:34
  • 137
    Careful, `subprocess.check_output()` returns a `bytes` object, not a `str`. If all you want to do is print the result, that won't make any difference. But if you want to use a method such as `myString.split("\n")` on the result, you'll have to first decode the `bytes` object: `subprocess.check_output(myParams).decode("utf-8")` for instance. – TanguyP Sep 29 '15 at 15:57
  • @MikeGraham Dear Mike, you may be able to help me out with [this post](http://stackoverflow.com/questions/36620656/controlling-a-python-script-from-another-script). Any advice would be awesome. Cheers –  Apr 14 '16 at 12:00
  • 35
    adding `universal_newlines=True` as a parameter helped me in Python 3 to get the string object. *If universal_newlines is True, they are opened in text mode with default encoding. Otherwise, they are opened as binary streams.* – Jonathan Komar May 31 '17 at 05:22
  • 1
    @TanguyP Is the output guaranteed to be utf-8 encoded? Or how would you know the encoding? – ImportanceOfBeingErnest Aug 13 '18 at 11:53
  • @MikeGraham I found a case where Popen with stdout is strictly better than check_output, namely with remote ssh commands. This line results in an error if grep returns nothing: a = subprocess.check_output(['ssh', '11.11.11.11', "netstat -ltnp | grep 54991"], shell=False) subprocess.CalledProcessError: Command '['ssh', '10.252.199.11', 'netstat -ltnp | grep 54991']' returned non-zero exit status 1. If I use subprocess.Popen then the error goes away and I can retrieve the empty string return value using communicate()[0]. – John Jiang Sep 10 '19 at 20:08
  • @JohnJiang If you're in a situation where you don't care about your output code, that might be an easier flow indeed. BTW, you can also access the output via the raised CalledProcessError's `output` attribute. Also BTW, [paramiko](http://www.paramiko.org/) provides some nice features for programs executing remote shell commands. – Mike Graham Sep 16 '19 at 03:12
  • @MikeGraham agree with all 3 statements! – John Jiang Sep 17 '19 at 21:20
  • It's too bad @TanguyP only has a comment - they should probably ask and self-answer that as a question to get some SO creds for all the upvotes. His answer is why I'm here... – Marvin Oct 24 '19 at 17:58
  • 1
    The standard since Python 3.5 is [subprocess.run()](https://docs.python.org/3/library/subprocess.html#subprocess.run), with capture_output=True and text=True. The result.stdout and result.stderr can be used later on. – Sylvain Jun 17 '20 at 04:52
  • @ImportanceOfBeingErnest I believe the encoding completely depends on the particular process you're running, so you'd have to check the code/documentation of that process. (sorry to reply ages later) – TanguyP Aug 22 '20 at 16:49
  • @Marvin thanks for the suggestion to post an answer! But I believe it would be most useful/fair if @MikeGraham edited his answer to include `.decode("utf-8")` (as suggested in my above comment) - if we assume the encoding is UTF-8. – TanguyP Aug 22 '20 at 16:56
53

This worked for me for redirecting stdout (stderr can be handled similarly):

from subprocess import Popen, PIPE
pipe = Popen(path, stdout=PIPE)
text = pipe.communicate()[0]

If it doesn't work for you, please specify exactly the problem you're having.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 7
    This does produce some weird object. When I convert it to string, it escapes whitespace like `\n`. – Tomáš Zato Nov 26 '16 at 18:13
  • 2
    Note that this doesn't check if the subprocess ran correctly. You probably want to check that `pipe.returncode == 0` after the last line too. – thakis Jun 08 '18 at 14:56
  • 4
    the reason this works is because `Popen` returns a tuple of `stdout` and `stderr`, so when you access `[0]` you're just grabbing the `stdout`. You could also do `text, err = pipe.communicate()` and then `text` will have what you expect – Jona Oct 29 '19 at 19:27
51

In Python 3.7+ you can use the new capture_output= keyword argument for subprocess.run:

import subprocess

p = subprocess.run(["echo", "hello world!"], capture_output=True, text=True)
assert p.stdout == 'hello world!\n'
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
erb
  • 14,503
  • 5
  • 30
  • 38
47

Python 2: http://docs.python.org/2/library/subprocess.html#subprocess.Popen

from subprocess import PIPE, Popen

command = "ntpq -p"
process = Popen(command, stdout=PIPE, stderr=None, shell=True)
output = process.communicate()[0]
print output

In the Popen constructor, if shell is True, you should pass the command as a string rather than as a sequence. Otherwise, just split the command into a list:

command = ["ntpq", "-p"]
process = Popen(command, stdout=PIPE, stderr=None)

If you need to read also the standard error, into the Popen initialization, you should set stderr to PIPE or STDOUT:

command = "ntpq -p"
process = subprocess.Popen(command, stdout=PIPE, stderr=PIPE, shell=True)
output, error = process.communicate()

NOTE: Starting from Python 2.7, you could/should take advantage of subprocess.check_output (https://docs.python.org/2/library/subprocess.html#subprocess.check_output).


Python 3: https://docs.python.org/3/library/subprocess.html#subprocess.Popen

from subprocess import PIPE, Popen

command = "ntpq -p"
with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
    output = process.communicate()[0].decode("utf-8")
    print(output)

NOTE: If you're targeting only versions of Python higher or equal than 3.5, then you could/should take advantage of subprocess.run (https://docs.python.org/3/library/subprocess.html#subprocess.run).

Paolo Rovelli
  • 9,396
  • 2
  • 58
  • 37
  • awesome this is what i was looking for – kRazzy R Nov 14 '19 at 14:10
  • Probably also notice the place in the documentation where it says "don't use `Popen()` if you can use the higher-level API functions". You want `check_output` or in Python 3.4+ `subprocess.run()` – tripleee Jun 24 '21 at 07:20
  • @tripleee the original question explicitly asks about _"Store output of subprocess.Popen call in a string"_... in any case, yes, according to the documentation the recommended approach is to _"use the run() function for all use cases it can handle. For more advanced use cases, the underlying Popen interface can be used directly"_. However, take in mind that `run()` was added starting from Python 3.5. – Paolo Rovelli Jun 25 '21 at 13:58
  • For completeness, I've added to my answer a final note about this. ;) – Paolo Rovelli Jun 25 '21 at 14:15
  • Is is not recommended to use `shell=True` – alper Aug 09 '21 at 12:33
  • @alper where did you read that? As far as I could see in the docs, there are indeed some security considerations (i.e. _"If the shell is invoked explicitly, via `shell=True`, it is the application’s responsibility to ensure that all whitespace and metacharacters are quoted appropriately to avoid shell injection vulnerabilities."_) but, depending on your use case and how the shell command is built, that doesn't necessarily make it "not recommended". – Paolo Rovelli Aug 10 '21 at 14:07
  • And the docs itself offers a possible solution: _"When using `shell=True`, the `shlex.quote()` function can be used to properly escape whitespace and shell metacharacters in strings that are going to be used to construct shell commands."_. – Paolo Rovelli Aug 10 '21 at 14:10
  • @PaoloRovelli Here (https://stackoverflow.com/a/13332300/2402577) please see the first comment. Also please see: https://stackoverflow.com/a/3172488/2402577 . `So invoking the shell invokes a program of the user's choosing and is platform-dependent. Generally speaking, avoid invocations via the shell.` – alper Aug 10 '21 at 14:21
  • @alper I see. The first point (i.e. separating input/output) is a good one. The second point (i.e. being platform-dependent), however, really depends on your use case. I mean, if your code is targeting a specific platform only or is running inside a container, you _might_ or _might not_ care about it. – Paolo Rovelli Nov 02 '21 at 17:08
26

Assuming that pwd is just an example, this is how you can do it:

import subprocess

p = subprocess.Popen("pwd", stdout=subprocess.PIPE)
result = p.communicate()[0]
print result

See the subprocess documentation for another example and more information.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
18

for Python 2.7+ the idiomatic answer is to use subprocess.check_output()

You should also note the handling of arguments when invoking a subprocess, as it can be a little confusing....

If args is just single command with no args of its own (or you have shell=True set), it can be a string. Otherwise it must be a list.

for example... to invoke the ls command, this is fine:

from subprocess import check_call
check_call('ls')

so is this:

from subprocess import check_call
check_call(['ls',])

however, if you want to pass some args to the shell command, you can't do this:

from subprocess import check_call
check_call('ls -al')

instead, you must pass it as a list:

from subprocess import check_call
check_call(['ls', '-al'])

the shlex.split() function can sometimes be useful to split a string into shell-like syntax before creating a subprocesses... like this:

from subprocess import check_call
import shlex
check_call(shlex.split('ls -al'))
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
  • 1
    Five years later, this question is still getting lots of love. Thanks for the 2.7+ update, Corey! – Mark Mar 11 '16 at 08:06
11

This works perfectly for me:

import subprocess
try:
    #prints results and merges stdout and std
    result = subprocess.check_output("echo %USERNAME%", stderr=subprocess.STDOUT, shell=True)
    print result
    #causes error and merges stdout and stderr
    result = subprocess.check_output("copy testfds", stderr=subprocess.STDOUT, shell=True)
except subprocess.CalledProcessError, ex: # error code <> 0 
    print "--------error------"
    print ex.cmd
    print ex.message
    print ex.returncode
    print ex.output # contains stdout and stderr together 
Patrick Wolf
  • 2,530
  • 2
  • 28
  • 27
10

This was perfect for me. You will get the return code, stdout and stderr in a tuple.

from subprocess import Popen, PIPE

def console(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    out, err = p.communicate()
    return (p.returncode, out, err)

For Example:

result = console('ls -l')
print 'returncode: %s' % result[0]
print 'output: %s' % result[1]
print 'error: %s' % result[2]
gravmatt
  • 428
  • 7
  • 17
10

The accepted answer is still good, just a few remarks on newer features. Since python 3.6, you can handle encoding directly in check_output, see documentation. This returns a string object now:

import subprocess 
out = subprocess.check_output(["ls", "-l"], encoding="utf-8")

In python 3.7, a parameter capture_output was added to subprocess.run(), which does some of the Popen/PIPE handling for us, see the python docs :

import subprocess 
p2 = subprocess.run(["ls", "-l"], capture_output=True, encoding="utf-8")
p2.stdout
ynux
  • 1,280
  • 14
  • 21
5

I wrote a little function based on the other answers here:

def pexec(*args):
    return subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0].rstrip()

Usage:

changeset = pexec('hg','id','--id')
branch = pexec('hg','id','--branch')
revnum = pexec('hg','id','--num')
print('%s : %s (%s)' % (revnum, changeset, branch))
mpen
  • 272,448
  • 266
  • 850
  • 1,236
3
 import os   
 list = os.popen('pwd').read()

In this case you will only have one element in the list.

Alex
  • 6,843
  • 10
  • 52
  • 71
2
import subprocess
output = str(subprocess.Popen("ntpq -p",shell = True,stdout = subprocess.PIPE, 
stderr = subprocess.STDOUT).communicate()[0])

This is one line solution

Kirti Kedia
  • 133
  • 6
1

The following captures stdout and stderr of the process in a single variable. It is Python 2 and 3 compatible:

from subprocess import check_output, CalledProcessError, STDOUT

command = ["ls", "-l"]
try:
    output = check_output(command, stderr=STDOUT).decode()
    success = True 
except CalledProcessError as e:
    output = e.output.decode()
    success = False

If your command is a string rather than an array, prefix this with:

import shlex
command = shlex.split(command)
Zags
  • 37,389
  • 14
  • 105
  • 140
0

Use check_output method of subprocess module

import subprocess

address = '192.168.x.x'
res = subprocess.check_output(['ping', address, '-c', '3'])

Finally parse the string

for line in res.splitlines():

Hope it helps, happy coding

YoavBZ
  • 178
  • 1
  • 9
Salman Mushtaq
  • 341
  • 4
  • 23
-1

For python 3.5 I put up function based on previous answer. Log may be removed, thought it's nice to have

import shlex
from subprocess import check_output, CalledProcessError, STDOUT


def cmdline(command):
    log("cmdline:{}".format(command))
    cmdArr = shlex.split(command)
    try:
        output = check_output(cmdArr,  stderr=STDOUT).decode()
        log("Success:{}".format(output))
    except (CalledProcessError) as e:
        output = e.output.decode()
        log("Fail:{}".format(output))
    except (Exception) as e:
        output = str(e);
        log("Fail:{}".format(e))
    return str(output)


def log(msg):
    msg = str(msg)
    d_date = datetime.datetime.now()
    now = str(d_date.strftime("%Y-%m-%d %H:%M:%S"))
    print(now + " " + msg)
    if ("LOG_FILE" in globals()):
        with open(LOG_FILE, "a") as myfile:
            myfile.write(now + " " + msg + "\n")
Pavel Niedoba
  • 1,554
  • 2
  • 19
  • 36