To get the output of ls, use stdout=subprocess.PIPE.

>>> proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
>>> output = proc.stdout.read()
>>> print output
bar
baz
foo

The command cdrecord --help outputs to stderr, so you need to pipe that indstead. You should also break up the command into a list of tokens as I've done below, or the alternative is to pass the shell=True argument but this fires up a fully-blown shell which can be dangerous if you don't control the contents of the command string.

>>> proc = subprocess.Popen(['cdrecord', '--help'], stderr=subprocess.PIPE)
>>> output = proc.stderr.read()
>>> print output
Usage: wodim [options] track1...trackn
Options:
    -version    print version information and exit
    dev=target  SCSI target to use as CD/DVD-Recorder
    gracetime=# set the grace time before starting to write to #.
...

If you have a command that outputs to both stdout and stderr and you want to merge them, you can do that by piping stderr to stdout and then catching stdout.

subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

As mentioned by Chris Morgan, you should be using proc.communicate() instead of proc.read().

>>> proc = subprocess.Popen(['cdrecord', '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> out, err = proc.communicate()
>>> print 'stdout:', out
stdout: 
>>> print 'stderr:', err
stderr:Usage: wodim [options] track1...trackn
Options:
    -version    print version information and exit
    dev=target  SCSI target to use as CD/DVD-Recorder
    gracetime=# set the grace time before starting to write to #.
...
Answer from moinudin on Stack Overflow
Top answer
1 of 3
169

To get the output of ls, use stdout=subprocess.PIPE.

>>> proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
>>> output = proc.stdout.read()
>>> print output
bar
baz
foo

The command cdrecord --help outputs to stderr, so you need to pipe that indstead. You should also break up the command into a list of tokens as I've done below, or the alternative is to pass the shell=True argument but this fires up a fully-blown shell which can be dangerous if you don't control the contents of the command string.

>>> proc = subprocess.Popen(['cdrecord', '--help'], stderr=subprocess.PIPE)
>>> output = proc.stderr.read()
>>> print output
Usage: wodim [options] track1...trackn
Options:
    -version    print version information and exit
    dev=target  SCSI target to use as CD/DVD-Recorder
    gracetime=# set the grace time before starting to write to #.
...

If you have a command that outputs to both stdout and stderr and you want to merge them, you can do that by piping stderr to stdout and then catching stdout.

subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

As mentioned by Chris Morgan, you should be using proc.communicate() instead of proc.read().

>>> proc = subprocess.Popen(['cdrecord', '--help'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> out, err = proc.communicate()
>>> print 'stdout:', out
stdout: 
>>> print 'stderr:', err
stderr:Usage: wodim [options] track1...trackn
Options:
    -version    print version information and exit
    dev=target  SCSI target to use as CD/DVD-Recorder
    gracetime=# set the grace time before starting to write to #.
...
2 of 3
49

If you are using python 2.7 or later, the easiest way to do this is to use the subprocess.check_output() command. Here is an example:

output = subprocess.check_output('ls')

To also redirect stderr you can use the following:

output = subprocess.check_output('ls', stderr=subprocess.STDOUT)



In the case that you want to pass parameters to the command, you can either use a list or use invoke a shell and use a single string.

output = subprocess.check_output(['ls', '-a'])
output = subprocess.check_output('ls -a', shell=True)
🌐
Earthly
earthly.dev › blog › python-subprocess
How to Use Python's Subprocess Module - Earthly Blog
July 11, 2023 - However, it may sometimes be more convenient to run system programs from within a Python script. Python’s subprocess module provides ready-to-use functionality to run external commands, capture and process outputs, redirect output to files and I/O streams, and much more!
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management — Python 3.15.2 documentation
This module is not supported on mobile platforms or WebAssembly platforms. The recommended approach to invoking subprocesses is to use the run() function for all use cases it can handle.
🌐
Digitaldesignjournal
digitaldesignjournal.com › python-subprocess-output-to-variable
Python Subprocess Output To Variable [Explained With Example]
September 24, 2023 - The result of subprocess.run() is stored in the result variable, which contains information about the command execution. We extract the standard output of the command from the result object using result.stdout.
🌐
Reddit
reddit.com › r/learnpython › is there a simple way to get output of subprocess.call into a variable?
r/learnpython on Reddit: Is there a simple way to get output of subprocess.call into a variable?
July 20, 2012 -

I'm very new to python. I've written many Bash scripts before, but I'm trying to write a script in python for the very first time.

Things were going well until I tried to get the results of a system command into a variable. I just assumed that this is how it is done:

VARIABLE = subprocess.call(command, shell=True)

Seems to me like that is how it SHOULD be done. But anyways, I've googled how to actually do it and it seems extremely convoluted and needlessly complex. I can't get it to work. Can someone help me out?

I'm using python 2.4

🌐
Medium
0xay.medium.com › how-to-store-the-output-of-a-command-in-a-variable-using-python-e7c1b8b91036
Store the Output of a Command in a Variable using Python. | by Ayush Sharma | Medium
January 7, 2024 - In this example, we use the `subprocess.check_output()` function to run the command `ls -l` and store its output in the `output` variable. By decoding the byte string, we can obtain the text representation of the output.
Find elsewhere
🌐
GitHub
gist.github.com › nawatts › e2cdca610463200c12eac2a14efc0bfb
capture-and-print-subprocess-output.py · GitHub
The only way to accomplish this seems to be to start the subprocess with the non-blocking subprocess.Popen, poll for available output, and both print it and accumulate it in a variable.
🌐
DataCamp
datacamp.com › home › tutorials › python › an introduction to python subprocess: basics and examples
An Introduction to Python Subprocess: Basics and Examples | DataCamp
April 23, 2026 - For example, it is now possible to use the output of the subprocess as a variable throughout the rest of the Python script. Gain the programming skills all Python Developers need. ... Let's take a look at when to use the Python subprocess module. The subprocess module can be used to automate various system tasks, such as running backups, starting and stopping services, and scheduling cron jobs.
🌐
Medium
medium.com › @piyush.jpg.32 › storing-output-of-a-command-in-a-variable-using-python-560261ac898a
Storing output of a command in a variable using python | by Piyush sharma | Medium
July 17, 2023 - The Python subprocess module can be used to store a command’s output in a variable. Using the subprocess.run() function, you can issue a command and record the results.
🌐
Stack Overflow
stackoverflow.com › questions › 76911575 › python-redirect-output-of-command-to-variable
subprocess - Python Redirect Output of command to Variable - Stack Overflow
So i need to use subrocess.call() to run my command. But is there a way to get the output of subrocess.call()? ... I want to Code a Minecraft launcher using minecraft-launcher-lib. The reason why i created this question is, to show the log of the Game in a special window. ... import subprocess command = ["echo", "Hello, World!"] result = subprocess.run(command, stdout=subprocess.PIPE, text=True) output = result.stdout.strip() print("Output:", output)
🌐
LinkedIn
linkedin.com › pulse › store-output-command-variable-using-python-harsh-joshi
To store the output of a command in a variable using Python
July 19, 2023 - python Copy code import subprocess ... output print(output) In the above example, the subprocess.check_output() function is used to run the command specified as a string....
🌐
Medium
medium.com › @s2442garg › store-the-output-of-a-command-in-a-variable-using-python-c47ba68d41a2
Store the output of a command in a variable using Python. | by shubham garg | Medium
July 16, 2023 - result = subprocess.run(command, shell=True, capture_output=True, text=True) 6. After running the command, you can check the return code of the command using result.returncode. A return code of 0 indicates that the command executed successfully, while a non-zero return code indicates an error. ... 7. If the return code is 0, you store the captured output in the output variable by accessing result.stdout.
🌐
Medium
medium.com › @singhalkushal01 › how-to-store-the-output-of-a-command-in-a-variable-using-python-fa4b8a45a2d2
How to store the output of a command in a variable using Python. | by Kushal Singhal | Medium
July 1, 2023 - To store the output of a command in a variable using Python, you can use the subprocess module. Here’s an example of how to achieve that: import subprocess # Command to pexecute command = "ls -l" # Run the command and capture the output output ...
🌐
Dataquest
dataquest.io › home › blog › python subprocess: the simple beginner's tutorial
Python Subprocess: The Simple Beginner's Tutorial (2023)
February 19, 2025 - It's very helpful, and, in fact, it's the recommended option when you need to run multiple processes in parallel or call an external program or external command from inside your Python code. One of the pros of the subprocess module is that it allows the user to manage the inputs, the outputs, and even the errors raised by the child process from the Python code. This possibility makes calling subprocesses more powerful and flexible — it enables using the output of the subprocess as a variable throughout the rest of the Python script, for instance.