24

I want to do subprocess.call, and get the output of the call into a string. Can I do this directly, or do I need to pipe it to a file, and then read from it?

In other words, can I somehow redirect stdout and stderr into a string?

alexgolec
  • 26,898
  • 33
  • 107
  • 159
  • maybe the same as http://stackoverflow.com/questions/2502833/python-store-output-of-subprocess-call-in-a-string – aeter May 05 '11 at 18:54
  • Possible duplicate of [Store output of subprocess.Popen call in a string](https://stackoverflow.com/questions/2502833/store-output-of-subprocess-popen-call-in-a-string) – thakis Jun 08 '18 at 14:54

5 Answers5

40

This is an extension of mantazer's answer to python3. You can still use the subprocess.check_output command in python3:

>>> subprocess.check_output(["echo", "hello world"])
b'hello world\n'

however now it gives us a byte string. To get a real python string we need to use decode:

>>> subprocess.check_output(["echo", "hello world"]).decode(sys.stdout.encoding)
'hello world\n'

Using sys.stdout.encoding as the encoding rather than just the default UTF-8 should make this work on any OS (at least in theory).

The trailing newline (and any other extra whitespace) can be easily removed using .strip(), so the final command is:

>>> subprocess.check_output(["echo", "hello world"]
                            ).decode(sys.stdout.encoding).strip()
'hello world'
dshepherd
  • 4,989
  • 4
  • 39
  • 46
  • How to write the output to a file? I tried passing the file name in command itself using redirect '>' but when next line in the python code tries to see the contents it's empty – Sanjeevkumar M Sep 06 '18 at 15:22
  • @SanjeevkumarM Use the [stdout](https://docs.python.org/3/library/subprocess.html#frequently-used-arguments) argument, give it a file *object* to write to (e.g. as created by [open](https://docs.python.org/3/library/functions.html#open), make sure you close it afterwards). – dshepherd Sep 06 '18 at 16:50
  • This was perfect. Thank you. – Bryce Wayne Jun 21 '21 at 23:29
11

No, you can't read the output of subprocess.call() directly into a string.

In order to read the output of a command into a string, you need to use subprocess.Popen(), e.g.:

>>> cmd = subprocess.Popen('ls', stdout=subprocess.PIPE)
>>> cmd_out, cmd_err = cmd.communicate()

cmd_out will have the string with the output of the command.

aeter
  • 11,960
  • 6
  • 27
  • 29
6

The subprocess module provides a method check_output() that runs the provided command with arguments (if any), and returns its output as a byte string.

output = subprocess.check_output(["echo", "hello world"])
print output

The code above will print hello world

See docs: https://docs.python.org/2/library/subprocess.html#subprocess.check_output

Muntaser Ahmed
  • 4,487
  • 1
  • 16
  • 17
  • How to write the output to a file? I tried passing the file name in command itself using redirect '>' but when next line in the python code tries to see the contents it's empty – Sanjeevkumar M Sep 06 '18 at 15:25
5

Using text=True in Python 3.7+

In newer versions of Python, you can simply use text=True to get a string return value:

>>> import subprocess
>>> subprocess.check_output(["echo", "hello"], text=True)
'hello\n'

Here is what the docs say:

If encoding or errors are specified, or text is true, file objects for stdin, stdout and stderr are opened in text mode using the specified encoding and errors or the io.TextIOWrapper default. The universal_newlines argument is equivalent to text and is provided for backwards compatibility. By default, file objects are opened in binary mode.

Flimm
  • 136,138
  • 45
  • 251
  • 267
2

subprocess.call() takes the same arguments as subprocess.Popen(), which includes the stdout and stderr arguments. See the docs for details.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Looking through docs online isn't very clear to me, could you provide an example, like the other answer using Popen. Or post link with a clear example of how to get stdout value when stdout=subprocess.PIPE for example. Is the answer the same as the Popen answer? – David Dec 30 '14 at 08:23
  • It is in fact the same, since both functions take the same arguments. – Ignacio Vazquez-Abrams Dec 30 '14 at 16:52