管道内缺陷分割python_从Python中的管道捕获的标准输出被截断

I want to capture the ouput of dpkg --list | grep linux-image in Python 2.6.5 on Ubuntu 10.04.

from subprocess import Popen

from subprocess import PIPE

p1 = Popen(["dpkg", "--list"], stdout=PIPE)

p2 = Popen(["grep", "linux-image"], stdin=p1.stdout, stdout=PIPE)

stdout = p2.communicate()[0]

The content of stdout is:

>>> print stdout

rc linux-image-2. 2.6.31-14.48 Linux kernel image for version 2.6.31 on x86

ii linux-image-2. 2.6.32-22.36 Linux kernel image for version 2.6.32 on x86

ii linux-image-2. 2.6.32-23.37 Linux kernel image for version 2.6.32 on x86

ii linux-image-2. 2.6.32-24.43 Linux kernel image for version 2.6.32 on x86

ii linux-image-2. 2.6.32-25.44 Linux kernel image for version 2.6.32 on x86

ii linux-image-ge 2.6.32.25.27 Generic Linux kernel image

However, this is not the same as running dpkg --list | grep linux-image in a shell:

cschol@blabla:~$ dpkg --list | grep linux-image

rc linux-image-2.6.31-14-generic 2.6.31-14.48 Linux kernel image for version 2.6.31 on x86

ii linux-image-2.6.32-22-generic 2.6.32-22.36 Linux kernel image for version 2.6.32 on x86

ii linux-image-2.6.32-23-generic 2.6.32-23.37 Linux kernel image for version 2.6.32 on x86

ii linux-image-2.6.32-24-generic 2.6.32-24.43 Linux kernel image for version 2.6.32 on x86

ii linux-image-2.6.32-25-generic 2.6.32-25.44 Linux kernel image for version 2.6.32 on x86

ii linux-image-generic 2.6.32.25.27 Generic Linux kernel image

Looking at the first line, one can see that the output in Python is truncated:

rc linux-image-2. 2.6.31-14.48

instead of

rc linux-image-2.6.31-14-generic 2.6.31-14.48

Why does it do that and is there a way to get exactly the same output in Python?

解决方案import subprocess

p1 = subprocess.Popen(["dpkg", "--list"], stdout=subprocess.PIPE, env={'LANG':'C'})

p2 = subprocess.Popen(["grep", "linux-image"], stdin=p1.stdout, stdout=subprocess.PIPE)

out,err=p2.communicate()

print(out)

The dpkg command's output depends on the value of the LANG environment variable.

Setting LANG=C in subprocess.Popen will make dpkg's output more like what you see from the terminal.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值