缺少子进程命令的输出 [英] Missing output from subprocess command

查看:26
本文介绍了缺少子进程命令的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,对本地项目运行 PEP8 样式检查.我尝试使用 subprocess 方法,我能够获取提示的生成终端输出以改进样式并将其保存为字符串.

I am working on a project to run PEP8 style check on local projects. I’ve tried to use the subprocess method and I am able to get the generated terminal output of the tips to improve style and save it to a string.

我生成PEP8样式的代码如下:

My code to generate the PEP8 style is as below:

def run_pep8_style(target):
    pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --filename=*.py " + target, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    tips = pep_tips.communicate()[0]

    print "Style: "
    print tips

但是,我尝试使用相同的子流程方法和存储来生成计数,但没有成功.终端显示输出,但未将其捕获到字符串变量中.

However, I have tried to generate the count using the same subprocess method and store but I am unsuccessful of doing it. The terminal display the output but it is not captured into the string variable.

def run_pep8_count(target):
    pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --count -qq --filename=*.py  " + target, shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    tips = pep_tips.communicate()[0]
    print "Count: "
    print tips

奇怪的是,我能够将终端文本中的样式列表存储到字符串变量中,但是当我尝试捕获 PEP8 计数时它不返回任何内容.计数的终端输出是否与样式列表不同?我是 Python 编程的新手,因此将不胜感激.谢谢.

The strange thing is that I am able to store the list of styles from terminal text into a string variable but it returns none when I try to capture the PEP8 count. Is the terminal output of the count differs from the list of styles? I am a novice to Python programming so any help would be appreciated. Thanks.

推荐答案

根据文档, pep8.py --count 打印到标准错误:

According to the docs, pep8.py --count prints to stderr:

--count              print total number of errors and warnings to standard
                       error and set exit code to 1 if total is not null

所以你需要告诉 subprocss.Popen 将 stderr 指向 subprocess.PIPE:

So you need to tell subprocss.Popen to direct stderr to subprocess.PIPE:

pep_tips = subprocess.Popen("python pep8.py --ignore=E111,E501 --count -qq          
               --filename=*.py  " + target, shell=False,
               stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
               stderr=subprocess.PIPE)

tips,tips_err = pep_tips.communicate()
print tips_err

这篇关于缺少子进程命令的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆