我想在python脚本中有三个不同的引号,因为我想在另一台计算机上执行两行python命令。例如:
import commands
command = "ssh someothercomputer 'python -c `import psutil; print psutil.cpu_percent()`'"
output = commands.getstatusoutput(command)[1]
但是,反引号不会被识别为引号。错误如下:
"Badly placed ()'s.\nArgument expected for the -c option\nusage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...\nTry `python -h' for more information."
我怎样才能让它发挥作用?
答案 0 :(得分:0)
您可以转义内部引号,而不是使用反引号,以便将它们放在command
中。像这样:
command = "ssh someothercomputer 'python -c \"import psutil; print psutil.cpu_percent()\"'"
使用commands
执行此命令会产生所需的输出(另一台计算机上的当前cpu百分比)