Popen 与 PIPE 通信无法捕获进程的所有输出
我正在尝试运行 python 脚本并捕获它的输出。似乎在第一个输出行之后,它重定向到控制台而不是我的字符串。 Manage.py 是一个用于管理 Django 项目的命令行实用程序,例如启动生产服务器或运行单元测试。
这是我的代码:
import os, string, datetime
from subprocess import Popen, PIPE
def runProcess(command, parameters):
process = Popen([command]+parameters, stdout=PIPE)
output=process.communicate()[0]
return output
testStatus=runProcess('python',['manage.py','test','coffeebean'])
print ("*****Own output*****")
print(testStatus)
这是输出:
Ran 1 test in 0.000s
OK
*****Own output*****
Creating test database for alias 'default'...
Destroying test database for alias 'default'...
为什么第一行没有被捕获?
此致, 丹尼尔
I'm trying to run a python script and capture the output of it. It seems like after the first output line it redirects to the console instead of to my string. Manage.py is a command-line utility for managing Django projects, like starting the production server or running unit tests.
This is my code:
import os, string, datetime
from subprocess import Popen, PIPE
def runProcess(command, parameters):
process = Popen([command]+parameters, stdout=PIPE)
output=process.communicate()[0]
return output
testStatus=runProcess('python',['manage.py','test','coffeebean'])
print ("*****Own output*****")
print(testStatus)
This is the output:
Ran 1 test in 0.000s
OK
*****Own output*****
Creating test database for alias 'default'...
Destroying test database for alias 'default'...
Why are the first lines not catched?
Best regards,
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为它们被写入 stderr,而不是 stdout。尝试
Because they're written to stderr, not stdout. Try