简体   繁体   English

在Python脚本的bash命令中使用三个不同的引号

[英]Use three different quotation marks in a bash command in Python scripts

I want to have three different quotation marks in a python script, because I want to execute a two-line python command, on a different computer. 我想在python脚本中使用三个不同的引号,因为我想在另一台计算机上执行两行python命令。 For example: 例如:

import commands
command = "ssh someothercomputer 'python -c `import psutil; print psutil.cpu_percent()`'"
output = commands.getstatusoutput(command)[1]

However, the backticks are not recognized as quotation marks. 但是,反引号不被识别为引号。 The error is the following: 错误如下:

"Badly placed ()'s.\nArgument expected for the -c option\nusage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...\nTry `python -h' for more information."

How can I get this to work? 我该如何工作?

Instead of using backticks, you can escape the inner quotation marks, to have them in command . 除了使用反引号,您还可以转义内部的引号,使它们在command Like this: 像这样:

command =  "ssh someothercomputer 'python -c \"import psutil; print psutil.cpu_percent()\"'"

Executing this command with commands yields the wanted output (the current cpu percentage at the other computer) 使用commands执行此commands产生所需的输出(另一台计算机上的当前cpu百分比)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM