25

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?

2

5 Answers 5

41

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'
3
  • 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 Sep 6, 2018 at 15:22
  • @SanjeevkumarM Use the stdout argument, give it a file object to write to (e.g. as created by open, make sure you close it afterwards).
    – dshepherd
    Sep 6, 2018 at 16:50
  • This was perfect. Thank you. Jun 21, 2021 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.

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

1
  • 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 Sep 6, 2018 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.

2

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

2
  • 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, 2014 at 8:23
  • It is in fact the same, since both functions take the same arguments. Dec 30, 2014 at 16:52

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.