【问题标题】:using Python's subprocess for reading the output of a blocking command sent through stdin使用 Python 的子进程读取通过标准输入发送的阻塞命令的输出
【发布时间】:2018-07-29 09:34:20
【问题描述】:

我有以下代码:

proc = subprocess.Popen(["adb", "shell"], stdout.subprocess.PIPE, subprocess.PIPE)
proc.stdin.write("cat /proc/kmsg")

该命令一直将数据输出到标准输出,我有兴趣在某个时间停止命令(CTRL-C)并读取整个输出数据。
现在我唯一能做的就是 proc.stdout.readline() 并希望下一行不是空的。
我有兴趣停止命令并阅读到目前为止我已经使用的所有行。

【问题讨论】:

    标签: python subprocess stdout stdin popen


    【解决方案1】:

    只需拨打proc.kill() 然后proc.communicate() 即可完成阅读。

    【讨论】:

    • 不客气。请考虑单击此答案左侧的复选标记以“接受”它。欢迎来到 SO。
    猜你喜欢
    • 1970-01-01
    • 2014-03-29
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多