在Python和windows7中使用“findstr”循环访问stdout命令行内容

2024-09-24 22:17:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个python脚本,它使用Windows命令行向设备发送命令,然后读取输出迭代其内容:

proc = subprocess.Popen("adb logcat | findstr Test", stdout=subprocess.PIPE)
for line in proc.stdout:
    if "stopped" in line:
        print line
        print "Test Service finished \n"
        break
    else:
        print line

不幸的是,我得到了以下信息:

^{pr2}$

如果没有“| findstr Gps”,它也可以工作,但是发送到管道的输出量太大,最终会导致内存事件。在

你知道我做错了什么吗?在

提前谢谢!在


Tags: 命令行intest命令脚本内容windowsstdout
1条回答
网友
1楼 · 发布于 2024-09-24 22:17:28

我认为这可能有用(不确定stderr=subprocess.PIPE是否真的有必要,尽管它不应该伤害):

proc = subprocess.Popen("adb logcat | findstr Test", stdout=subprocess.PIPE,
                                                     stderr=subprocess.PIPE))
stdoutdata, stderrdata = proc.communicate()
for line in stdoutdata:
    if "stopped" in line:
        print line
        print "Test Service finished \n"
        break
    else:
        print line

相关问题 更多 >