我试图使用Willie将命令的输出返回给IRC通道。
我的代码似乎可以逐行输出变量,但由于某种原因,一旦我使用威利机器人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:发布于 2015-02-01 01:14:31
我将代码改为使用以下代码:
process = subprocess.Popen(command,shell=True, stderr=subprocess.PIPE)
output = process.stderr.read()这个问题现在解决了。
https://stackoverflow.com/questions/28258169
复制相似问题