python-如何使用popen传递输出? [英] python - how to pipe the output using popen?

查看:192
本文介绍了python-如何使用popen传递输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用popen输出文件的pipe,我该怎么做?

I want to pipe output of my file using popen, how can I do that?

test.py :

while True:
  print"hello"

a.py :

import os  
os.popen('python test.py')

我想使用os.popen用管道传输输出. 我该怎么办?

I want to pipe the output using os.popen. how can i do the same?

推荐答案

首先,不建议使用os.popen(),而应使用子进程模块.

First of all, os.popen() is deprecated, use the subprocess module instead.

您可以像这样使用它:

from subprocess import Popen, PIPE

output = Popen(['command-to-run', 'some-argument'], stdout=PIPE)
print output.stdout.read()

这篇关于python-如何使用popen传递输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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