Redirecting stdio from a command in os.system () in Python

Usually I can change stdout in Python by changing the sys.stdout value. However, this only affects print expressions. So, is there a way to suppress the output (to the console) of a program that is executed using the os.system() command in Python?

+7
python stdout stdio os.system
Jul 07 '10 at 17:59
source share
4 answers

You may have run the program through subprocess.Popen with the message subprocess.PIPE , and then drag this output whenever you want, but as it 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 can see the full documentation: Python subprocess module

+14
Jul 07 '10 at 18:09
source share

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") 
+12
Jul 07 '10 at 18:07
source share

Redirect stderr as well as stdout.

0
Jul 07 2018-10-10T00:
source share

If you want to completely exclude the console launched with the python program, you can save it with the extension .pyw.

Perhaps I do not understand the question.

0
Jul 07 '10 at 18:09
source share



All Articles