从 Python 中的管道捕获的标准输出被截断

发布于 2024-09-26 01:04:42 字数 1979 浏览 7 评论 0原文

我想捕获 dpkg --list | 的输出Ubuntu 10.04 上的 Python 2.6.5 中的 grep linux-image。

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]

stdout 的内容是:

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

但是,这与运行 dpkg --list | 不同。在 shell 中运行 grep linux-image:

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

查看第一行,可以看到 Python 中的输出被截断:

rc  linux-image-2. 2.6.31-14.48

而不是

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 just the same output in 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那请放手 2024-10-03 01:04:42
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)

dpkg 命令的输出取决于 LANG 环境变量的值。
subprocess.Popen 中设置 LANG=C 将使 dpkg 的输出更像您从终端看到的内容。

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.

葬﹪忆之殇 2024-10-03 01:04:42

不需要使用 grep !

import subprocess
p1 = subprocess.Popen(["dpkg", "--list"], stdout=subprocess.PIPE, env={'LANG':'C'})
out,err=p1.communicate()
for o in  out.split("\n"):
    if "linux-image" in o:
        print o

There is no need to use grep !

import subprocess
p1 = subprocess.Popen(["dpkg", "--list"], stdout=subprocess.PIPE, env={'LANG':'C'})
out,err=p1.communicate()
for o in  out.split("\n"):
    if "linux-image" in o:
        print o
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文