Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent subprocess.Popen from displaying output in python

So I am trying to store the output of a command into a variable. I do not want it to display output while running the command though...

The code I have right now is as follows...

def getoutput(*args):
    myargs=args
    listargs=[l.split(' ',1) for l in myargs]
    import subprocess
    output=subprocess.Popen(listargs[0], shell=False ,stdout=subprocess.PIPE)   
    out, error = output.communicate()
    return(out,error)


def main():

    a,b=getoutput("httpd -S")

if __name__ == '__main__':
    main()

If I put this in a file and execute it on the command line. I get the following output even though I do not have a print statement in the code. How can I prevent this, while still storing the output?

#python ./apache.py 
httpd: Could not reliably determine the server's fully qualified domain name, using xxx.xxx.xxx.xx for ServerName
Syntax OK
like image 691
user1601716 Avatar asked Dec 24 '12 16:12

user1601716


People also ask

How do I turn off subprocess output in Python?

As of Python3 you no longer need to open devnull and can call subprocess. DEVNULL. Works! In addition, can swap stderr with stdout in code above (or append as another argument) to suppress outputs.

Do you need to close Popen?

Popen do we need to close the connection or subprocess automatically closes the connection? Usually, the examples in the official documentation are complete. There the connection is not closed. So you do not need to close most probably.

How do I capture the output of a 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.

How do I stop subprocess calls?

Popen(args) with args as a sequence of program arguments or a single string to execute a child program in a new process with the supplied arguments. To terminate the subprocess, call subprocess. Popen. terminate() with subprocess.


1 Answers

What you are seeing is standard-error output, not standard-output output. Stderr redirection is controlled by the stderr constructor argument. It defaults to None, which means no redirection occurs, which is why you see this output.

Usually it's a good idea to keep stderr output since it aids debugging and doesn't affect normal redirection (e.g. | and > shell redirection won't capture stderr by default). However you can redirect it somewhere else the same way you do stdout:

sp = subprocess.Popen(listargs[0], shell=False,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
output, error = sp.communicate()

Or you can just drop stderr:

devnull = open(os.devnull, 'wb') #python >= 2.4
sp = subprocess.Popen(listargs[0], shell=False,
    stdout=subprocess.PIPE, stderr=devnull)

#python 3.x:
sp = subprocess.Popen(listargs[0], shell=False
    stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
like image 120
Francis Avila Avatar answered Sep 28 '22 20:09

Francis Avila