Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a quiet version of subprocess.call?

Is there a variant of subprocess.call that can run the command without printing to standard out, or a way to block out it's standard out messages?

like image 340
fergusdawson Avatar asked Dec 16 '11 03:12

fergusdawson


People also ask

What is the difference between subprocess call and Popen?

Popen is more general than subprocess. call . Popen doesn't block, allowing you to interact with the process while it's running, or continue with other things in your Python program. The call to Popen returns a Popen object.

What is difference between OS system and subprocess call?

os. system is equivalent to Unix system command, while subprocess was a helper module created to provide many of the facilities provided by the Popen commands with an easier and controllable interface. Those were designed similar to the Unix Popen command.

Does subprocess call wait for completion?

The subprocess module provides a function named call. This function allows you to call another program, wait for the command to complete and then return the return code.

What is CWD in subprocess?

subprocess. Popen takes a cwd argument to set the Current Working Directory; you'll also want to escape your backslashes ( 'd:\\test\\local' ), or use r'd:\test\local' so that the backslashes aren't interpreted as escape sequences by Python. The way you have it written, the \t part will be translated to a tab .


2 Answers

Yes. Redirect its stdout to /dev/null.

process = subprocess.call(["my", "command"], stdout=open(os.devnull, 'wb'))
like image 69
Matt Joiner Avatar answered Oct 16 '22 09:10

Matt Joiner


Often that kind of chatter is coming on stderr, so you may want to silence that too. Since Python 3.3, subprocess.call has this feature directly:

To suppress stdout or stderr, supply a value of DEVNULL.

Usage:

import subprocess
rc = subprocess.call(args, stderr=subprocess.DEVNULL, stdout=subprocess.DEVNULL)

If you are still on Python 2:

import os, subprocess

with open(os.devnull, 'wb') as shutup:
    rc = subprocess.call(args, stdout=shutup, stderr=shutup)
like image 51
wim Avatar answered Oct 16 '22 10:10

wim