subprocess.check_output() 没有输出 [英] No output from subprocess.check_output()

查看:71
本文介绍了subprocess.check_output() 没有输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Python 2.7.5 中运行以下代码:

I'm trying to run the following code in Python 2.7.5:

output = subprocess.check_output(commandList)
print (len(output))

我的命令列表是一个参数列表,例如:['ls', '-l'].

My command list is a list of arguments like: ['ls', '-l'].

但是,即使我有一个很长的输出字符串,我也得到输出的长度为 0.check_output 虽然工作正常,因为我可以看到在标准输出(控制台)中运行的进程的结果.

But then I get that the length of output is 0 even when I have a very long output string. The check_output though works correctly because I can see the result of the process running in the stdout (console).

理想的用例是运行子进程、隐藏标准输出(在控制台中看不到任何内容)、解析输出字符串并提取一些相关信息.

The ideal use case would be to run the subprocess, hide the stdout (nothing seen in the console), parse the output string and extract some relevant information.

推荐答案

这应该可以工作,但是您正在运行的实际命令(可能不是 ls)可能有问题并且正在发送它的所有输出到 stderr 而不是 stdout.试试

This is supposed to work, but it's possible that the actual command you're running (presumably not ls) is buggy and sending all of its output to stderr instead of stdout. Try

output = subprocess.check_output(commandList, stderr=subprocess.STDOUT)

这篇关于subprocess.check_output() 没有输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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