I think the problem is with the statement for line in proc.stdout, which reads the entire input before iterating over it. The solution is to use readline() instead:

#filters output
import subprocess
proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)
while True:
  line = proc.stdout.readline()
  if not line:
    break
  #the real code does filtering here
  print "test:", line.rstrip()

Of course you still have to deal with the subprocess' buffering.

Note: according to the documentation the solution with an iterator should be equivalent to using readline(), except for the read-ahead buffer, but (or exactly because of this) the proposed change did produce different results for me (Python 2.5 on Windows XP).

Answer from Rômulo Ceccon on Stack Overflow
🌐
Python
docs.python.org › 3 › library › subprocess.html
subprocess — Subprocess management
5 days ago - Special value that can be used as the stdin, stdout or stderr argument to Popen and indicates that a pipe to the standard stream should be opened. Most useful with Popen.communicate(). ... Special value that can be used as the stderr argument ...
Discussions

How to get output from subprocess.Popen along with visible execution?
I am trying to get the output by running below dummy code for my project - #test.py import time for x in range(5): print(i) time.sleep(1) This is test2.py import subprocess cmd = "python3 test.py" process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=true, text=true) ... More on discuss.python.org
🌐 discuss.python.org
6
1
July 22, 2024
Python subprocess output to stdout - Stack Overflow
@josh: Yes, that's expected, and the reason why the code does not store the result in a variable. Since we don't send the output to a pipe, the subprocess will inherit stdout from the current process, meaning everything will be printed to the terminal directly. More on stackoverflow.com
🌐 stackoverflow.com
subprocess.Popen and output
To avoid deadlock, I think you are supposed to say handbrake.communicate() if using pipes. But I'm not familiar with your technique of using pipes to suppress output, either. IME, pipes are for capturing output. There's a DEVNULL object for suppression. It's either stdout or stderr, at least on a UNIX. More on reddit.com
🌐 r/learnpython
18
22
December 7, 2022
Capturing output from subprocess.run()

You’ll need to use the stdout and/or stderr arguments. https://docs.python.org/3/library/subprocess.html#subprocess.run

Edit: link to #subprocess.run

More on reddit.com
🌐 r/learnpython
5
7
January 29, 2018
🌐
GitHub
gist.github.com › nawatts › e2cdca610463200c12eac2a14efc0bfb
capture-and-print-subprocess-output.py · GitHub
By default, subprocess.run does not capture any output, but the subprocess does print to the terminal. Passing stdout=subprocess.PIPE, stderr=subprocess.STDOUT to subprocess.run captures the output but does not let the subprocess print.
🌐
DataCamp
datacamp.com › tutorial › python-subprocess
An Introduction to Python Subprocess: Basics and Examples | DataCamp
September 12, 2025 - The subprocess.run() method takes several arguments, some of which are: args: The command to run and its arguments, passed as a list of strings. capture_output: When set to True, will capture the standard output and standard error. text: When set to True, will return the stdout and stderr as string, otherwise as bytes.
🌐
Computer Science Atlas
csatlas.com › python-subprocess-run-stdout-stderr
Python 3: Get Standard Output and Standard Error from subprocess.run() — Computer Science Atlas
July 14, 2021 - On Python 3.7 or higher, if we pass in capture_output=True to subprocess.run(), the CompletedProcess object returned by run() will contain the stdout (standard output) and stderr (standard error) output of the subprocess:
🌐
Earthly
earthly.dev › blog › python-subprocess
How to Use Python's Subprocess Module - Earthly Blog
July 11, 2023 - To run an external command within a Python script, use subprocess.run(command), where command is a string or a list of strings (when running commands with arguments). The run() function returns a CompletedProcess object. You can set capture_output = True to capture the output as a string of bytes in the stdout attribute, which you can decode by calling the decode() method.
Find elsewhere
🌐
Alexwlchan
alexwlchan.net › til › 2025 › subprocess-line-by-line
How to stream lines from stdout with subprocess – alexwlchan
import subprocess args = ["yt-dlp", "--get-id", "https://www.youtube.com/watch?list=PLCbA9r6ecYWU6SVyvb32a0YHIzpr9jxnW"] proc = subprocess.Popen(args, stdout=subprocess.PIPE, bufsize=1, text=True) for line in proc.stdout: print(line) proc.poll() print(proc.returncode)
🌐
SaltyCrane
saltycrane.com › blog › 2008 › 09 › how-get-stdout-and-stderr-using-python-subprocess-module
How to get stdout and stderr using Python's subprocess module - SaltyCrane Blog
from subprocess import Popen, PIPE, STDOUT cmd = 'ls /etc/fstab /etc/non-existent-file' p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True) output = p.stdout.read() print output
🌐
End Point Dev
endpointdev.com › blog › 2015 › 01 › getting-realtime-output-using-python
Getting realtime output using Python Subprocess | End Point Dev
January 28, 2015 - def run_command(command): process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE) while True: output = process.stdout.readline() if output == '' and process.poll() is not None: break if output: print output.strip() rc = process.poll() return rc · shell python ·
🌐
Lucadrf
lucadrf.dev › blog › python-subprocess-buffers
Luca Da Rin Fioretto | Capture Python subprocess output in real-time
November 20, 2022 - Unfortunately Python’s official documentation doesn’t offer any alternative solution, “There should be one – and preferably only one – obvious way to do it.” the Zen of Python says, although using Popen.communicate() to read the stdout of a piped subprocess is all but obvious.
🌐
Python Module of the Week
pymotw.com › 2 › subprocess
subprocess – Work with additional processes - Python Module of the Week
import subprocess output = subprocess.check_output( 'echo to stdout; echo to stderr 1>&2; exit 1', shell=True, stderr=subprocess.STDOUT, ) print 'Have %d bytes in output' % len(output) print output · Now the error and standard output channels are merged together so if the command prints error messages, they are captured and not sent to the console. $ python subprocess_check_output_error_trap_output.py Traceback (most recent call last): File "subprocess_check_output_error_trap_output.py", line 15, in <mo dule> stderr=subprocess.STDOUT, File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.
🌐
Real Python
realpython.com › python-subprocess
The subprocess Module: Wrapping Programs With Python – Real Python
January 18, 2025 - The interface, the shell, and the REPL share the streams: You can think of the standard I/O streams as byte dispensers. The subprocess fills up stdout and stderr, and you fill up stdin.
🌐
Reddit
reddit.com › r/learnpython › subprocess.popen and output
r/learnpython on Reddit: subprocess.Popen and output
December 7, 2022 -

I'm trying to grok subprocess.Popen to run and monitor a command line executable in the background. (Specifically, the HandbrakeCLI video converter.)

In this I have been partially successful, using the following command:

handbrake = subprocess.Popen( cmd )

Where cmd is a list of parameters. When I do this, I can do other things while it's running, poll() it, and terminate it if desired, and unless I kill it, it runs to completion exactly the way I want. The problem is that I also want to suppress the output. No problem, right?

handbrake = subprocess.Popen( cmd, stdout=subprocess.PIPE )

This works, but there's still SOME output. Now, my first thought was that the messages I was seeing were techincally ERROR messages. So I tried two different methods:

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

In both cases, the subprocess launches, I can do other things, but... it doesn't do anything. I can see HandbrakeCLI in the task manager, but it's not using any resources (where it *should* be using nearly 100%), and no file has been created in the target directory.

This leaves me with two questions that may or may not be related:

  1. Why is redirecting stdout causing the program to do nothing?

  2. Where is that other output coming from, and how do I suppress it? (Helpfully, it doesn't contain any information I need to capture.)

🌐
Runebook.dev
runebook.dev › en › docs › python › library › subprocess › subprocess.CompletedProcess.stdout
Mastering Subprocess.stdout in Python Concurrent Execution
This method reads all the output from both stdout and stderr pipes simultaneously and closes the pipes, ensuring the child process can finish. import subprocess import threading from subprocess import Popen, PIPE def run_concurrent_task(command): ...
🌐
Sololearn
sololearn.com › en › Discuss › 1220762 › how-to-deal-with-multiple-subprocess-and-read-stdout-while-process-running-
How to deal with multiple subprocess and read stdout while process running ? | Sololearn: Learn to code for FREE!
Solved: cmd = "adb logcat | grep KeyCode" proc_logcat = subprocess.Popen(cmd.split(' '), stdout=subprocess.PIPE) proc_script = subprocess.check_call(['./adb_keycode_test.sh']) os.kill(proc_logcat.pid, signal.SIGTERM) text = proc_logcat.stdout.read()
🌐
Arch Linux Forums
bbs.archlinux.org › viewtopic.php
Python3: get stdout of a command in real time / Programming & Scripting / Arch Linux Forums
December 21, 2023 - After doing some research, I was able to find a working solution using Python's pty.openpty function and specifying the file descriptors in subprocess.Popen. The implementation looks something like this: stdout_fd, stdout_slave=pty.openpty() stderr_fd, stderr_slave=pty.openpty() process=subprocess.Popen(command, stdin=stdout_slave, stdout=stdout_slave, stderr=stderr_slave, bufsize=0, close_fds=True, env=env) prev_attrs=termios.tcgetattr(sys.stdin) def get_terminal_size(): return fcntl.ioctl(0, termios.TIOCGWINSZ, struct.pack('HHHH',0,0,0,0)) last_terminal_size=struct.pack('HHHH',0,0,0,0) # pla