【问题标题】:Trying to call a shell command with Python, gets nothing尝试用 Python 调用 shell 命令,一无所获
【发布时间】:2013-09-16 15:53:57
【问题描述】:

我正在尝试使用 casperjs 在我的网络应用程序上运行一些集成测试。为此,我有以下测试类:

from django.conf import settings
import unittest
import subprocess

class MyTest(unittest.TestCase):
    def test_something(self):
        print "begin"
        command = "../../n1k0-casperjs-76fc831/bin/casperjs test.js"
        p = subprocess.Popen(command, shell=True, bufsize=0, stdout=subprocess.PIPE, universal_newlines=True)
        p.wait()
        output = p.stdout.read()
        p.stdout.close()
        print output
        print "end"

当我在 django 或 classif python shell 中执行此代码时,我得到了js 脚本的输出。但是,当它用 django 调用时,我得到了这个:

begin

end

你们知道为什么会这样吗?

【问题讨论】:

  • 顺便说一句,当你重定向到管道时不要调用 p.wait() 。如果管道充满,这可能会挂起。将管道读到底,然后等待。

标签: python django casperjs


【解决方案1】:

你的shell命令是否可能实际上输出到stderr?

要将 stderr 重定向到 stdout,请尝试调用以下命令:

p = subprocess.Popen(command, shell=True, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 1970-01-01
    • 2021-01-25
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    相关资源
    最近更新 更多