在Python脚本中的bash命令中使用三个不同的引号
我想在python脚本中使用三个不同的引号,因为我想在不同的计算机上执行两行python命令。例如:在Python脚本中的bash命令中使用三个不同的引号,python,bash,ssh,Python,Bash,Ssh,我想在python脚本中使用三个不同的引号,因为我想在不同的计算机上执行两行python命令。例如: import commands command = "ssh someothercomputer 'python -c `import psutil; print psutil.cpu_percent()`'" output = commands.getstatusoutput(command)[1] 但是,反勾号不能识别为引号。错误如下: "Badly placed ()'s.\nArgum
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."
如何使其工作?不使用反勾号,您可以将内部引号转义,使其位于
命令中。像这样:
command = "ssh someothercomputer 'python -c \"import psutil; print psutil.cpu_percent()\"'"
使用命令执行此命令
产生所需的输出(另一台计算机上的当前cpu百分比)