如何在python脚本中获取exe的输出? [英] How to get output of exe in python script?

查看:1093
本文介绍了如何在python脚本中获取exe的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在Python中调用外部 .exe 程序时,如何从<$获取 printf 输出c $ c> .exe 应用程序并将其打印到我的Python IDE?

When I call an external .exe program in Python, how can I get printf output from the .exe application and print it to my Python IDE?

推荐答案

打电话给Python的外部程序,使用子进程模块。

To call an external program from Python, use the subprocess module.


子进程模块允许您生成新进程,连接到它们的输入/输出/错误管道,并获取它们的返回代码。

The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

doc中的一个例子(输出是一个提供输出的文件对象来自子进程。):

An example from the doc (output is a file object that provides output from the child process.):

output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0]

一个具体的例子,使用 cmd ,带有2个参数的Windows命令行解释器:

A concrete example, using cmd, the Windows command line interpreter with 2 arguments:

>>> p1 = subprocess.Popen(["cmd", "/C", "date"],stdout=subprocess.PIPE)
>>> p1.communicate()[0]
'The current date is: Tue 04/14/2009 \r\nEnter the new date: (mm-dd-yy) '
>>> 

这篇关于如何在python脚本中获取exe的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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