[英]python - how to pipe the output using popen?
我想用popen
pipe
输出我的文件,我怎么能这样做?
test.py :
while True:
print"hello"
a.py :
import os
os.popen('python test.py')
我想使用os.popen
管道输出。 我怎么能这样做?
首先,不推荐使用os.popen(),而是使用子进程模块。
你可以像这样使用它:
from subprocess import Popen, PIPE
output = Popen(['command-to-run', 'some-argument'], stdout=PIPE)
print output.stdout.read()
使用subprocess
模块,这是一个例子:
from subprocess import Popen, PIPE
proc = Popen(["python","test.py"], stdout=PIPE)
output = proc.communicate()[0]
这将只打印第一行输出:
a.py:
import os
pipe = os.popen('python test.py')
a = pipe.readline()
print a
......这将打印所有这些
import os
pipe = os.popen('python test.py')
while True:
a = pipe.readline()
print a
(我将test.py改为了这个,以便更容易看到发生了什么:
#!/usr/bin/python
x = 0
while True:
x = x + 1
print "hello",x
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.