Python os.system without the output
Solution 1
Avoid os.system()
by all means, and use subprocess instead:
with open(os.devnull, 'wb') as devnull:
subprocess.check_call(['/etc/init.d/apache2', 'restart'], stdout=devnull, stderr=subprocess.STDOUT)
This is the subprocess
equivalent of the /etc/init.d/apache2 restart &> /dev/null
.
There is subprocess.DEVNULL
on Python 3.3+:
#!/usr/bin/env python3
from subprocess import DEVNULL, STDOUT, check_call
check_call(['/etc/init.d/apache2', 'restart'], stdout=DEVNULL, stderr=STDOUT)
Solution 2
Depending on your OS (and that's why as Noufal said, you should use subprocess instead) you can try something like
os.system("/etc/init.d/apache restart > /dev/null")
or (to mute also the error)
os.system("/etc/init.d/apache restart > /dev/null 2>&1")
Solution 3
You should use the subprocess
module using which you can control the stdout
and stderr
in a flexible fashion. os.system
is deprecated.
The subprocess
module allows you to create an object which represents a running external process. You can read it from it's stdout/stderr, write to it's stdin, send it signals, terminate it etc. The main object in the module is Popen
. There are a bunch of other convenience methods like call etc. The docs are very comprehensive and include a section on replacing the older functions (including os.system
).
Solution 4
Here is a system call function I pieced together several years ago and have used in various projects. If you don't want any output from the command at all you can just say out = syscmd(command)
and then do nothing with out
.
Tested and works in Python 2.7.12 and 3.5.2.
def syscmd(cmd, encoding=''):
"""
Runs a command on the system, waits for the command to finish, and then
returns the text output of the command. If the command produces no text
output, the command's return code will be returned instead.
"""
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT,
close_fds=True)
p.wait()
output = p.stdout.read()
if len(output) > 1:
if encoding: return output.decode(encoding)
else: return output
return p.returncode
user697108
Updated on September 27, 2021Comments
-
user697108 over 2 years
I'm running this:
os.system("/etc/init.d/apache2 restart")
It restarts the webserver, as it should, and like it would if I had run the command directly from the terminal, it outputs this:
* Restarting web server apache2 ...
waiting [ OK ]
However, I don't want it to actually output it in my app. How can I disable it? Thanks!
-
Kirk Strauser about 13 years
os.system("/etc/init.d/apache2 restart >/dev/null")
will discard that output. As Noufal has said,subprocess
is preferred. If you're wanting to make a quick adjustment to pre-existing code, though, redirecting to /dev/null might be an attractive option. -
mb14 about 13 years@kirk: why a comment rather than a answer ?
-
Kirk Strauser about 13 years@mb14: I didn't think it was as "correct" as the recommendations to use subprocess. I thought of it as more of a side note, like "while I'm not exactly suggesting you do this, here's another idea."
-
jfs over 8 years
-
ChathuraG over 7 yearsthis worked for me because i was looking for something which works in python 2 and 3.
-
Sushila Jyothi Lévêque about 4 yearsYou can also see is their is a quiet or silent command line option. This, however, is not garunteed, and the program may still output stderr.
-
-
user697108 about 13 yearsAwesome. Thanks for answering. Which function would it be though? The docs confuse me. call? popen?
-
user225312 about 13 years+10 for an incomplete answer! This shows that voting and quality don't go hand in hand :-) . Noufal, you could mention the differences.
-
Noufal Ibrahim about 13 years
popen
but you should take some time and read through the docs. I imagine you've only skimmed through them. They're quite clear really. -
Kirk Strauser about 13 yearsDo you have a source explicitly marking it as deprecated, as opposed to "not preferred"? Guido is on record as being opposed to its removal. I'm not disagreeing with your answer - subprocess is so much nicer! - just clarifying a point.
-
Noufal Ibrahim about 13 yearsGuido is generally against changing the standard library. The docs for
os.system
indicate that it's "preferable" to use subprocess. -
Noufal Ibrahim about 13 yearsA A: I think it was fairly complete. All that was needed was a pointer to the right way. I'm surprised at the upvotes though. I've updated it with some more information.
-
mb14 about 13 years@AA: I think when the answer is to redirect to a doc, a quick answer is a good answer. I think Noufal answer is a good answer then (even though indeed not enough to be accepted).
-
Noufal Ibrahim about 13 years+1 for an actual example that shows how to solve the OPs problem using
subprocess
. :)