如何使用子进程运行此命令行语句?

时间:2019-06-27 11:45:35

标签: python python-3.x subprocess

如何在子流程中运行这句话?

./deb -address  12.345.66.3:6000 -file ./234.csv  -key "-key" -secret "password" -id "1234" -insert line


subprocess.call(["","","",""])  but  when "" comes like here in key how to use its `"" -key ""` or `'" -key "'`

需要输入

1 个答案:

答案 0 :(得分:2)

您可以直接在shell中运行整个命令,方法是使用单引号引起来并使用shell=True参数:

subprocess.call('./deb -address  12.345.66.3:6000 -file ./234.csv  -key "-key" -secret "password" -id "1234" -insert line', shell=True)

但是我建议您利用 shlex.split 进行拆分并为您生成正确转义的命令列表,以便无需使用shell即可运行它:

import shlex
command = shlex.split('./deb -address  12.345.66.3:6000 -file ./234.csv  -key "-key" -secret "password" -id "1234" -insert line')
subprocess.call(command)

FWIW,在受支持的版本上,您应该使用subprocess.run而不是subprocess.call(和兄弟姐妹)。