I can execute a terminal command using os.system()
but I want to capture the output of this command. How can I do this?
Asked
Active
Viewed 1.6e+01k times
47

Marcello B.
- 4,177
- 11
- 45
- 65

AssemblerGuy
- 603
- 2
- 6
- 12
4 Answers
51
>>> import subprocess
>>> cmd = [ 'echo', 'arg1', 'arg2' ]
>>> output = subprocess.Popen( cmd, stdout=subprocess.PIPE ).communicate()[0]
>>> print(output)
arg1 arg2
There is a bug in using of the subprocess.PIPE. For the huge output use this:
import subprocess
import tempfile
with tempfile.TemporaryFile() as tempf:
proc = subprocess.Popen(['echo', 'a', 'b'], stdout=tempf)
proc.wait()
tempf.seek(0)
print(tempf.read())

Ajeet Verma
- 2,938
- 3
- 13
- 24

Jiří Polcar
- 1,192
- 9
- 15
-
1you are my savior!!! i have been looking for something like this for days!!! thank you! – RAZ_Muh_Taz Sep 22 '17 at 18:13
33
The recommended way in Python 3.5 and above is to use subprocess.run()
:
from subprocess import run
output = run("pwd", capture_output=True).stdout

Sven Marnach
- 574,206
- 118
- 941
- 841
-
-
@Cherona It's deifned in the `subprocess` module, so you need to import it. – Sven Marnach Apr 29 '20 at 19:25
-
-
How can I explicitly open read from a terminal? (If stdin has already been redirected to reading from a shell pipe?) – Helen Craigman Oct 07 '20 at 10:14
-
@HelenCraigman I don't think that's possible. If stdin is not connected to a terminal, what terminal would you like to read from? – Sven Marnach Oct 07 '20 at 12:22
-
@sven When I pipe to a python program (for example, in ubuntu: `cat file.txt | python3 program.py`, I would like to have the python program read lines from "file.txt", and interact with the user at the terminal – Helen Craigman Oct 08 '20 at 16:36
-
@sven (Or, actually in my case: `script.sh | python3 program.py`). stdin reads lines from the pipe, and I need the user to interact with the Python program. – Helen Craigman Oct 08 '20 at 16:43
-
1@HelenCraigman I see. On Unix, you can still read from the "controlling terminal" by opening `/dev/tty` for reading and writing. I'm not sure that pattern is a good idea, though. – Sven Marnach Oct 08 '20 at 19:44
-
1FileNotFoundError: [WinError 2] The system cannot find the file specified – fourk Feb 21 '21 at 09:54
-
On Win10 and Python 3.7.9. this now results in the line `output = run("pwd", capture_output=True).stdout` failing with error: `FileNotFoundError: [WinError 2] The system cannot find the file specified`. – lazarea Jan 17 '22 at 21:30
-
@lazarea On Windows, there is no `pwd` executable. The name of the executable is just an example. – Sven Marnach Jan 18 '22 at 14:18
15
You can use Popen in subprocess
as they suggest.
with os
, which is not recomment, it's like below:
import os
a = os.popen('pwd').readlines()

gerry
- 1,539
- 1
- 12
- 22
-
This does not work. `Popen` objects have no `readlines()` method. – Sven Marnach Dec 10 '10 at 11:38
-
-
6
0
The easiest way is to use the library commands
import commands
print commands.getstatusoutput('echo "test" | wc')

Mahmoud
- 539
- 4
- 6
-
2Where do you get the commands module? It doesn't appear to be on pip for Python3. – Brōtsyorfuzthrāx May 04 '14 at 00:20
-
2@Shule commands is an older module. It is replaced by the subprocess module. https://docs.python.org/2/library/subprocess.html#subprocess.check_output – aaroh Jun 25 '18 at 06:21