【问题标题】:Use three different quotation marks in a bash command in Python scripts在 Python 脚本的 bash 命令中使用三个不同的引号
【发布时间】:2015-03-23 18:45:35
【问题描述】:

我想在 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."

我怎样才能让它工作?

【问题讨论】:

    标签: python bash ssh


    【解决方案1】:

    您可以转义内引号,而不是使用反引号,将它们放在command 中。像这样:

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

    使用commands 执行此命令会产生所需的输出(另一台计算机上的当前 cpu 百分比)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 1970-01-01
      相关资源
      最近更新 更多