5

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
user5740843
  • 1,540
  • 5
  • 22
  • 42
  • 1
    Can you try using `subprocess` as described here? http://stackoverflow.com/questions/11269575/how-to-hide-output-of-subprocess-in-python-2-7 – John Zwinck Apr 24 '16 at 16:18

1 Answers1

8

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')
jordanm
  • 33,009
  • 7
  • 61
  • 76