繁体   English   中英

Python 子进程 - 返回非零退出状态 1

[英]Python subprocess - returned non-zero exit status 1

我想使用python 3.6subprocess进程模块执行这个UNIX命令:

 cat report_html | ./wkhtmltopdf - test.pdf

我试过的

import subprocess
report_html = "<html><body><h1>Hello World</h1></body></html>"
subprocess.run(['cat', report_html, '|', '/tmp/wkhtmltopdf', '-', '/tmp/test.pdf'], check=True)

我得到的错误

{
  "errorMessage": "Command '['cat', '<html><body><h1>Hello World</h1></body></html>', '|', '/tmp/wkhtmltopdf', '-', '/tmp/test.pdf']' returned non-zero exit status 1.",
  "errorType": "CalledProcessError",
  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      30,
      "lambda_handler",
      "subprocess.run(['cat', report_html, '|', '/tmp/wkhtmltopdf', '-', '/tmp/test.pdf'], check=True)"
    ],
    [
      "/var/lang/lib/python3.6/subprocess.py",
      438,
      "run",
      "output=stdout, stderr=stderr)"
    ]
  ]
}

请指导我如何解决这个问题。


如果要在 subprocess 中使用subprocess ,则必须使用shell=True参数,由于多种原因,这不是最佳解决方案。

通常,这是通过运行两个单独的进程并将第一个进程的 output 传递给第二个进程来完成的,例如:

cat = subprocess.Popen(['cat', 'report_html'], stdout=subprocess.PIPE)  # Change stdout to PIPE
output = subprocess.check_output(['wkhtmltopdf', '-', 'xxx.pdf'], stdin=cat.stdout)  # Get stdin from cat.stdout

暂无
暂无

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

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