bollytje2 bollytje2 - 7 months ago 16
Bash Question

Execute shell command from Python Script

I want to execute this command from a python script:

iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f > scan.txt


I tried like the following

from subprocess import call
call(["iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f > scan.txt"])


but I get an error

SyntaxError: EOL while scanning string literal


How can I do that?

Answer

Pass shell=True to subprocess.call:

call("iw wlan0 scan | sed -e 's#(on wlan# (on wlan#g' | awk -f scan.txt", shell=True)

Note that shell=True is not a safe option always.