Python suppress shell output [duplicate]
Asked Answered
M

1

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?

Mitran answered 24/4, 2016 at 16:13 Comment(1)
Can you try using subprocess as described here? #11270075Holding
C
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')
Choir answered 24/4, 2016 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.