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?
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.
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.
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.
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 .
Yes. Redirect its stdout
to /dev/null
.
process = subprocess.call(["my", "command"], stdout=open(os.devnull, 'wb'))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With