Python子进程Popen stdout只变量

时间:2014-10-16 08:29:41

标签: python subprocess popen

我想做一些简单的事情,但我没有抓住......我在Google上搜索了很多,而且我找不到任何东西!

我这样做:

from subprocess import Popen, PIPE

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

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

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日志文件!

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

致以最诚挚的问候,

1 个答案:

答案 0 :(得分:1)

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

试试这个。