使用Willie bot输出不起作用,但打印确实有效

时间:2015-02-01 00:34:24

标签: python bots irc

我正在尝试使用Willie bot将命令的输出返回到IRC通道。

我的代码似乎可以逐行输出我的变量,但出于某种原因,一旦我利用Willie机器人say命令输出到IRC,它就不会输出任何内容。

这是我的代码:

from willie import module
import subprocess
import urllib2
import os


@module.commands('splint')
def splint(bot, trigger):
    bot.reply('I will process your request now!')
    page = urllib2.urlopen(trigger.group(2))
    page_content = page.read();

    with open('codeToCheck.c', 'w') as code:
        code.write(page_content)

    command = 'splint "C:\Users\Justin\Desktop\codeToCheck.c"'
    output = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]

    bot.say('I have saved the file successfully. Outputting:')

    for i in output.splitlines():
        bot.say(i)

    bot.say(output)

在这里使用我的小测试代码我已经确定它适用于print:

import subprocess,os

output = subprocess.Popen(["splint", "C:\cygwin64\home\Justin\codeToCheck.c"], stdout=subprocess.PIPE).communicate()[0]

command = 'splint "C:\Users\Justin\Desktop\codeToCheck.c"'
output = subprocess.Popen(command,shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE).communicate()[0]

for i in output.splitlines():
    print i

print 'I have saved the file successfully. Outputting:'

这就是我的代码的irc输出:

<Fogest> .splint http://pastebin.com/raw.php?i=8cB7DdnQ
<Fogbot> Fogest: I will process your request now!
<Fogbot> I have saved the file successfully. Outputting:

应该有输出,但什么都没有。我在这里做错了吗?通过命令行运行我的测试文件(我在这篇文章中显示的测试代码),我得到了以下输出:

$ python test.py
Splint 3.1.2 --- 25 Aug 2010

Finished checking --- no warnings
I have saved the file successfully. Outputting:

1 个答案:

答案 0 :(得分:0)

我将代码改为使用以下代码:

process = subprocess.Popen(command,shell=True, stderr=subprocess.PIPE)
output = process.stderr.read()

问题现已解决。