Usually I can change stdout in Python by changing the value of sys.stdout
. However, this only seems to affect print
statements. So, is there any way I can suppress the output (to the console), of a program that is run via the os.system()
command in Python?

- 119,891
- 44
- 235
- 294

- 21,580
- 20
- 67
- 100
5 Answers
On a unix system, you can redirect stderr and stdout to /dev/null as part of the command itself.
os.system(cmd + "> /dev/null 2>&1")

- 3,677
- 1
- 25
- 26
-
This was the answer I was looking for and should've been the accepted answer. – spurra Apr 10 '19 at 18:43
You could consider running the program via subprocess.Popen
, with subprocess.PIPE
communication, and then shove that output where ever you would like, but as is, os.system
just runs the command, and nothing else.
from subprocess import Popen, PIPE
p = Popen(['command', 'and', 'args'], stdout=PIPE, stderr=PIPE, stdin=PIPE)
output = p.stdout.read()
p.stdin.write(input)
Much more flexible in my opinion. You might want to look at the full documentation: Python Subprocess module

- 3,718
- 3
- 22
- 24
-
Mmm...okay. So than the command is executed on the line P = Popen(...), yes? And it will only show the output when calling p.stdout.read()...yes? Thank you – Leif Andersen Jul 07 '10 at 18:46
-
Okay...the commands did run, but in a separate thread. Is there anyway I can either hault the program while the commands run, or keep it in the same thread? Thank you. – Leif Andersen Jul 07 '10 at 18:54
-
2Simply, use p.wait(). However, apparently this can result in deadlocking when using PIPE stdout if the program generates enough output. See the full documentation at http://docs.python.org/library/subprocess.html#subprocess.Popen.wait . However, I think it should work.. – Blue Peppers Jul 07 '10 at 19:02
-
Hmm...okay, thanks. Also, it appears that using p.communicate() (rather than p.stdout will help clear the pipe. Thanks. – Leif Andersen Jul 07 '10 at 19:16
-
p.communicate() should be used instead of read() and write() to avoid deadlocks where the command is waiting for input to finish. Try this >>> p = Popen(['cat'], stdout=PIPE, stderr=PIPE, stdin=PIPE) >>> p.stdout.read() – Piotr Czapla Jul 22 '22 at 13:53
Redirect stderr as well as stdout.

- 22,868
- 20
- 88
- 147
-
2That don't do the job quote from http://docs.python.org/library/os.html#process-management os.system(command) Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command. – Xavier Combelle Jul 07 '10 at 18:06
If you want to completely eliminate the console that launches with the python program, you can save it with the .pyw extension.
I may be misunderstanding the question, though.

- 613
- 7
- 18
I've found this answer while looking for a pythonic way to execute commands.
The accepted answer has a deadlock if command waits for input. But it is not enough to just reverse read with write as you will have the deadlock if command writes larger output before it reads. The way to avoid this issue is to use p.communicate
or even better Popen.run
that does it for you.
Here are some examples:
To discard or read outputs:
>>> import subprocess
>>> r = subprocess.run("pip install fastai".split(), capture_output=True)
# you can ignore the result to discard it or read it
>>> r.stdout
# and gets stdout as bytes
To pass something to the process use input:
>>> import subprocess
>>> subprocess.run("tr h H".split(), capture_output=True, text=True, input="hello").stdout
'Hello'
If you really don't want to deal with stdout, you can pass subprocess. DEVNULL to stdout and stderr as follows:
>>> import subprocess as sp
>>> sp.run("pip install fastai".split(), stdout=sp.DEVNULL, stderr=sp.DEVNULL).returncode
0

- 25,734
- 24
- 99
- 122
-
Don't use `split` for commands. Use [shlex.split](https://docs.python.org/3/library/shlex.html#shlex.split) to avoid troubles. – Francisco Puga Apr 14 '23 at 17:33