python – 缺少subprocess命令的输出

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

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

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

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

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计数时它返回none.计数的终端输出是否与样式列表不同?我是Python编程的新手,所以任何帮助将不胜感激.谢谢.

最佳答案 根据
the docs,pep8.py –count打印到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:

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
点赞