Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress output from subprocess.Popen

Tags:

How do you stop the output from subprocess.Popen from being output? Printing can sometimes be slow if there is a great deal of it.

like image 427
coffee Avatar asked Aug 16 '11 17:08

coffee


People also ask

How do I hide subprocess output?

To hide output of subprocess with Python, we can set stdout to subprocess. DEVNULL`. to output the echo command's output to dev null by setting the stdout to subprocess. DEVNULL when we call subprocess.

How do you store output of subprocess run?

To capture the output of the subprocess. run method, use an additional argument named “capture_output=True”. You can individually access stdout and stderr values by using “output. stdout” and “output.

What is Popen in subprocess?

The subprocess module defines one class, Popen and a few wrapper functions that use that class. The constructor for Popen takes arguments to set up the new process so the parent can communicate with it via pipes. It provides all of the functionality of the other modules and functions it replaces, and more.


1 Answers

If you want to totally throw it away:

import subprocess import os with open(os.devnull, 'w') as fp:     cmd = subprocess.Popen(("[command]",), stdout=fp) 

If you are using Python 2.5, you will need from __future__ import with_statement, or just don't use with.

like image 173
Brent Newey Avatar answered Oct 13 '22 03:10

Brent Newey