【问题标题】:subprocess() arguments in PythonPython 中的 subprocess() 参数
【发布时间】:2011-04-25 16:29:23
【问题描述】:

我在 Python 中有一个参数搞砸了我的 subprocess() 命令。论据是:

--server-args="-screen 0, 1280x800x24"


args = [
  'xvfb-run',
  '--server-args="-screen 0, 1280x800x24"',
  '/usr/bin/python',
  '/root/AdamN-python-webkit2png-3ae4322/webkit2png.py',
  '-o',
  filename,
  url,
]

我认为它正在转义双引号。有解决办法吗?

【问题讨论】:

  • 它看起来不像是在向我转义双引号。
  • 这应该是列表中两个不同的元素,同样-o 和filename 是不同的。
  • args = [ 'xvfb-run', '--server-args=', '"-screen=0, 1280x800x24"', '/usr/bin/python', '/root/ AdamN-python-webkit2png-3ae4322/webkit2png.py', '-o', 文件名, 'google.com', ] /usr/bin/xvfb-run: line 173: "-screen=0, 1280x800x24": command not找到

标签: python subprocess command-line-arguments


【解决方案1】:

虽然您可能在过去两年中发现了这一点,但我今天也遇到了同样的问题。解决办法:

import subprocess
subprocess.check_call(['xvfb-run', '-s', '-screen 0 1024x768x24', 
                       'CutyCapt', 
                       '--url=http://www.google.com/',
                       '--out=google.png'])

或

import subprocess
subprocess.check_call(['xvfb-run', '--server-args=-screen 0 1024x768x24', 
                       'CutyCapt', 
                       '--url=http://www.google.com/',
                       '--out=google4.png'])

假设您已安装 xvfb。我使用CutyCapt 作为我的示例应用程序,它需要一个 X 帧缓冲区才能运行(它是一个将 webkit 页面转换为图像并需要一个 X 服务器的程序)。

【讨论】:

    【解决方案2】:

    这是 Python 代码,而不是 shell 命令行。

    shell 命令行使用引号来保留空格 - 在 Python 中,空格以不同的方式保留,因此引号按原样传递并成为被调用程序实际看到的参数的一部分。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2021-06-20
      • 2014-03-14
      • 2014-09-30
      • 2022-01-05
      • 2011-12-16
      • 1970-01-01
      • 2017-09-16
      相关资源
      最近更新 更多