如何在python中调用外部程序并检索输出和返回代码? [英] How to call an external program in python and retrieve the output and return code?

查看:107
本文介绍了如何在python中调用外部程序并检索输出和返回代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python脚本调用外部程序并检索输出并返回代码?

How can I call an external program with a python script and retrieve the output and return code?

推荐答案

查看 子流程 模块:下面是一个简单的示例...

Look at the subprocess module: a simple example follows...

from subprocess import Popen, PIPE

process = Popen(["ls", "-la", "."], stdout=PIPE)
(output, err) = process.communicate()
exit_code = process.wait()

这篇关于如何在python中调用外部程序并检索输出和返回代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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