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?
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?
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')
© 2022 - 2024 — McMap. All rights reserved.
subprocess
as described here? #11270075 – Holding