如何在子流程中运行这句话?
./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 "'`
需要输入
答案 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
(和兄弟姐妹)。