当我在 Python 中调用外部 .exe
程序时,如何从 .exe
应用程序中获取 printf
输出并将其打印到我的 Python IDE?
原文由 user53670 发布,翻译遵循 CC BY-SA 4.0 许可协议
当我在 Python 中调用外部 .exe
程序时,如何从 .exe
应用程序中获取 printf
输出并将其打印到我的 Python IDE?
原文由 user53670 发布,翻译遵循 CC BY-SA 4.0 许可协议
我很确定您在这里谈论的是 Windows(基于您问题的措辞),但在 Unix/Linux(包括 Mac)环境中,命令模块也可用:
import commands
( stat, output ) = commands.getstatusoutput( "somecommand" )
if( stat == 0 ):
print "Command succeeded, here is the output: %s" % output
else:
print "Command failed, here is the output: %s" % output
commands 模块提供了一个极其简单的界面来运行命令并获取状态(返回代码)和输出(从 stdout 和 stderr 读取)。或者,您可以分别通过调用 commands.getstatus() 或 commands.getoutput() 来获取状态或输出。
原文由 Shane C. Mason 发布,翻译遵循 CC BY-SA 2.5 许可协议
2 回答5.2k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1.4k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
3 回答1.3k 阅读✓ 已解决
2 回答884 阅读✓ 已解决
1 回答1.8k 阅读✓ 已解决
要从 Python 调用外部程序,请使用 subprocess 模块。
文档中的一个示例(
output
是一个提供子进程输出的文件对象。):一个具体的例子,使用
cmd
,带有 2 个参数的 Windows 命令行解释器: