Python子进程Popen stdout仅用于变量 [英] Python subprocess Popen stdout to variable only

查看:100
本文介绍了Python子进程Popen stdout仅用于变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做些简单的事,但我没听懂……我在Google上进行了很多搜索,但没有找到任何东西!

I want to do something easy, but I don't catch... I did many search on Google and I didn't find something!

我正在这样做:

from subprocess import Popen, PIPE

p1 = Popen(["fping", '-C10', '-B1', '-p500',
'-r1', '-s', '172.29.172.106'], stdout=PIPE)
output = p1.communicate()

因此,python执行我的命令,通讯等待返回代码,它工作正常.但是,我不知道为什么它显示输出...像这样:

So, python executes my command, communicate waits the return code, it works fine. But, I don't know why it displays the output... Like this :

172.29.172.106 : 13.50 18.56 13.22 13.42 13.39 187.62 13.34 13.51 13.36 26.35

       1 targets
       1 alive
       0 unreachable
       0 unknown addresses

       0 timeouts (waiting for response)
      10 ICMP Echos sent
      10 ICMP Echo Replies received
       0 other ICMP received

 13.2 ms (min round trip time)
 32.6 ms (avg round trip time)
 187 ms (max round trip time)
        4.530 sec (elapsed real time)

我只想执行一个命令,然后将输出重定向到变量,但是我不想显示输出,因为它会淹没我的crontab日志文件!

I just want to execute a command then redirect the output to a variable but I don't want to display the output because it floods my crontab log file!

所以问题是:如何使用popen(或其他函数)执行命令并将输出重定向到变量而不显示它?

So the question : how to use popen (or other function) to execute a command and redirect output to a variable without display it?

最诚挚的问候,

推荐答案

import subprocess 
p1 = Popen(["fping", '-C10', '-B1', '-p500','-r1', '-s', '172.29.172.106'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
output,error = p1.communicate()

尝试一下.

这篇关于Python子进程Popen stdout仅用于变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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