我想在Ubuntu 10.04的Python 2.6.5中捕獲dpkg --list | grep linux-image
的輸出。在Python中從管道捕獲的Stdout被截斷
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]
標準輸出的內容是:
>>> 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
然而,這是不一樣的shell中運行dpkg --list | grep linux-image
:
[email protected]:~$ 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
爲什麼會這樣,是有辦法讓Python中完全相同的輸出?
這解決了我的問題。謝謝!這是你必須知道的東西,還是在任何地方都有記錄? 「man dpkg」根本沒有提到LANG。 – cschol 2010-10-03 02:44:15
@cschol:不幸的是,我不知道任何文檔;我碰巧在這裏瞭解這個問題:http://ubuntuforums.org/showthread.php?p=6943778#post6943778。 – unutbu 2010-10-03 12:18:27