获取python子进程的输出 [英] Getting the output of a python subprocess

查看:51
本文介绍了获取python子进程的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在此处捕获输出.如果在 python 提示符下,我运行

p = subprocess.Popen(["/path/to/search_by_hash.par", hash_str],标准输出=子进程.PIPE)

返回值(一个列表)打印到标准输出,但没有被捕获

[4460475, 4406612, 4379510]

我试过跟进

value = p.communicate()[0]价值

..but value 是一个空字符串,不是我期待的整数列表,它被打印到 stdout

我尝试了来自 存储子进程输出的解决方案.Popen 在字符串中调用,但无法捕获输出.

更新:

stderr 似乎也没有产生任何东西......我正在寻找的列表正在被打印出来......只是没有任何运气捕获它.见下文:

<预><代码>>>>p = subprocess.Popen(["/home/jfry/tools/search_by_hash.par", hash_str],标准输出=子进程.PIPE)>>>[4460475、4406612、4379510]值,错误 = p.communicate()>>>价值''>>>呃

谢谢!

解决方案

尝试使用 p.communicate()[1] 检查 stderr.

I'm trying to capture the output here. If, at the python prompt, I run

p = subprocess.Popen(["/path/to/search_by_hash.par", hash_str], 
                      stdout=subprocess.PIPE)

The return value (a list) prints to stdout, but isn't captured

[4460475, 4406612, 4379510]

I've tried following it up with

value = p.communicate()[0]
value

..but value is an empty string, not the list of ints I was expecting, and which is being printed to stdout

I experimented with the solutions from Store output of subprocess.Popen call in a string but haven't been able to capture the output.

UPDATE:

stderr doesn't seem to yield anything either...and the list I'm looking for is being printed out...just not having any luck in capturing it. See below:

>>> p = subprocess.Popen(["/home/jfry/tools/search_by_hash.par", hash_str], 
                          stdout=subprocess.PIPE)
>>> 
[4460475, 4406612, 4379510]
    value, err  = p.communicate()
>>> value
''
>>> err

Thanks!

解决方案

Try checking stderr with p.communicate()[1].

这篇关于获取python子进程的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆