简体   繁体   English

在标准输出中从底部打印第三行

[英]Print 3rd line from the bottom in stdout

Currently i am running this 目前我正在运行

while p.poll() is None:
    output = p.stdout.readline()
    print output,

What is happening is all the lines are being printed. 正在发生的事情是所有行都被打印。 What i am looking for is to print, usually the 3rd line from the bottom, which contains data on whether the command has executed properly or not. 我正在寻找的是打印,通常从底部开始的第三行 ,其中包含有关命令是否已正确执行的数据。

Additionally is there a way to have like a status bar at the bottom that updates real time using Tkinter Label 此外,还有一种方法可以让底部的状态栏像使用Tkinter Label实时更新

Solution, thanks to @TadhgMcDonald-Jensen tip 解决方案,感谢@ TadhgMcDonald-Jensen提示

To print only the third last line (indice -3) in terminal shell window 仅在终端外壳窗口中打印第三行(索引-3)

import Tkinter as *
import subprocess

out = []  # Create empty list
for line in iter(p.stdout.readline, ''):
    out.append(line.rstrip('\n')) # append list
print out[-3] # print item in the list, in this case -3

Or to display in tkinter label 或以tkinter标签显示

import Tkinter as *
import subprocess

out = []  # Create empty list
for line in iter(p.stdout.readline, ''):
    out.append(line.rstrip('\n')) # append list
window = Tk()
Label(window, text=out[-3]).grid(sticky=W)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM