python - 从 Python 脚本执行 shell 命令

标签 python bash shell

我想从 python 脚本执行这个命令:

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

我试过如下

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

但是我得到一个错误

SyntaxError: EOL while scanning string literal

我该怎么做?

最佳答案

shell=True 传递给 subprocess.call:

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

请注意,shell=True 并不总是一个安全的选项。

关于python - 从 Python 脚本执行 shell 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36690810/

相关文章:

python - 如何根据 pandas 或 numpy 中的条件从数组中提取值?

linux - 如何使这个 AWK 命令不区分大小写?

bash - 在 Bash 注销时保存最后一个工作目录

bash - 重定向输入时找不到命令错误

python - Pandas Dataframe groupby : apply several lambda functions at once

python - 如何在 Python 中进行 scp?

Python:沿特定维度查找大于阈值的最大数组索引

linux - 如何使用 grep 精确匹配单词

Bash:如何将文件夹中的所有内容 move 到一个级别?

bash - 我怎样才能直接ssh到一个特定的目录?