Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python suppress shell output [duplicate]

My Python script is calling a shell command via os.system as:

os.system('sudo ifdown wlan0 &> /dev/null')

If I run this command without Python, the output is suppressed, in Python, however, it still prints the output.

What am I doing wrong?

like image 568
user5740843 Avatar asked Apr 24 '16 16:04

user5740843


1 Answers

When you use os.system, the shell used is /bin/sh. On many operating systems, /bin/sh is not bash. The redirection you are using, &>, is not defined by POSIX and will not work on some shells such as dash, which is /bin/sh on Debian and many of its derivatives. The following should correctly suppress the output:

os.system('sudo ifdown wlan0 > /dev/null 2>&1')
like image 105
jordanm Avatar answered Sep 25 '22 03:09

jordanm