当我在 Python 中调用外部 .exe
程序时,如何从 .exe
应用程序中获取 printf
输出并将其打印到我的 Python IDE?
最佳答案
要从 Python 调用外部程序,请使用 subprocess模块。
The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.
文档中的示例(output
是一个提供子进程输出的文件对象。):
output = subprocess.Popen(["mycmd", "myarg"], stdout=subprocess.PIPE).communicate()[0]
一个具体的例子,使用 cmd
,带有 2 个参数的 Windows 命令行解释器:
>>> 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 - 如何在python脚本中获取exe的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/748028/