Reputation: 65530
I'm using Python's subprocess.call
(docs here) and I have a question about standard output.
I have the following command:
subprocess.call("wget http://google.com", shell=True)
It works great, but it prints the output of wget, so I get an absolute ton of wget output like:
--2013-06-28 08:54:47-- http://google.com/
Resolving google.com... 173.194.41.100, 173.194.41.98, 173.194.41.97, ...
Connecting to google.com|173.194.41.100|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
.... etc
How can I change the call so it only prints anything if there is an error?
I have tried studying the subprocess docs, but I think I'm too inexperienced to figure it out for myself.
Upvotes: 0
Views: 1082
Reputation: 180917
wget
normally reports all output on stderr, so redirecting stdout will not help with getting errors only.
You can either use -nv
(non verbose) or -q
(quiet) as options to wget, the difference being that -q
does not even report errors while -nv
reports errors or a single success line to stderr.
The return code is unchanged in both cases, so it can be used to detect any errors if that's the way you prefer.
EDIT: If you really just want to ignore all output from wget, you can redirect stderr
and stdout
to a null device;
import subprocess
import os
DEVNULL = open(os.devnull, 'wb')
subprocess.call("wget http://google.com/", stdout=DEVNULL, stderr=DEVNULL, shell=True)
In my Python 3.3.1 install, DEVNULL can be imported from subprocess instead, but I can't find documentation on exactly what version it was added in. The above works in both Python2 and Python3.
Upvotes: 1
Reputation: 26
Actually I think the easiest way would be to use the '-q' option that 'wget' has. You can still catch error by looking at the return value.
Upvotes: 1