Capture output via subprocess w/o using communicate(通过子进程捕获输出,但不使用COMPLICATE)
本文介绍了通过子进程捕获输出,但不使用COMPLICATE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用子进程在Python脚本中调用外部程序。外部程序产生了大量的输出。我需要捕获该程序的输出。当前代码如下所示:
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"
')
while process.poll() != None:
line = process.stdout.readline()
print line
我在此代码中收到的错误是
进程试图写入不存在的管道。
如果我使用以下代码:
process = subprocess.Popen('cmd.exe', shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=None)
process.stdin.write('gams "indus89.gms"
')
o, e = process.communicate()
print o
则不捕获程序的输出。
我应该如何更改代码,以便可以在第三方程序运行时捕获其输出?
推荐答案
打开有点矫枉过正。
尝试:
output = subprocess.check_output('gams "indus89.gms"
', shell=True)
希望这能在您的环境中发挥作用。
这篇关于通过子进程捕获输出,但不使用COMPLICATE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:通过子进程捕获输出,但不使用COMPLICATE


基础教程推荐
猜你喜欢
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01